国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

WinForm中的ListBox組件編程(1)

2019-11-17 05:12:51
字體:
來源:轉載
供稿:網友
 ListBox組件是一個程序設計中經常使用到的組件,在Visual C#和Visual Basic .Net程序中使用這個組件,必須要在程序中導入.Net FrameWork SDK中名稱空間System.Windows.Forms,因為在System.Windows.Forms名稱空間中定義了這個組件。在asp.net的Web頁面中,ListBox組件是作為一個服務器端組件的形式出現的,所謂服務器端組件就是這些組件是在服務器端存在的。本文就是來介紹ListBox組件在ASP.NET的Web頁面中的具體使用和操作方法。

一. 如何在ASP.NET頁面中定義一個ListBox組件: 

在ASP.NET頁面中創建一個ListBox組件的語法如下:

<asp:ListBox Id = "MyListBox" runat = "server" >

<asp:ListItem Value = "1" >第一個條目</asp:ListItem >

<asp:ListItem Value = "2" >第二個條目</asp:ListItem >

注釋:這里還可以加入類似上面的若干條目

.....

</asp:ListBox >

在Web頁面中執行上面的語句就可以產生一個名稱為"MyListBox",包含若干條目的ListBox組件。

二. ListBox組件中常用的屬性:

我們通過以下表格來說明ListBox組件的一些常用的屬性:

屬性名稱 屬性代表的意義 SelectionMode 組件中條目的選擇的類型即:多選、單選。Single,Multiple Rows 此組件顯示總共多少行 Selected 檢測條目十分被選中 SelectedItem 返回的類型是ListItem,獲得組件中被選擇的條目 Count 組件中條目的總數 SelectedIndex 組件中被選擇的條目的索引值 Items 泛指組件中所有的條目,每一個條目的類型都是ListItem

三. 通過一個例子來把握ListBox組件在ASP.NET頁面中的具體用法:

在下面介紹ListBox組件在ASP.NET中的使用方法的時候,程序采用的程序設計語言是Visual C#。

(1).如何在ListBox組件添加新的條目:

通過以下語句就可以在名稱為lstItem的ListBox組件中增加一個名稱為"Sample"的條目:

lstItem . Items . Add ( new ListItem ( "Sample" ) ) 

(2).如何在ListBox組件中刪除指定的條目:

下列語句就是刪除名稱為lstItem的ListBox組件中的選定的一個條目:

  lstItem . Items . Remove ( lstItem . SelectedItem )

(3).如何在組件中移動指向條目的指針: 

移動條目的指針主要有四種方式:至首條目、至尾條目、下一條、上一條。在程序設計中主要是通過操作組件的Count和SelectedIndex屬性來實現以上四種方式的。以下就是具體實現這四種方式的程序代碼:

//按鈕"至首條"事件處理程序

if ( sender == First )

{

if ( lstItem . Items . Count > 0 )

{

lstItem . SelectedIndex = 0 ;

}

}

//按鈕"至尾條"事件處理程序

if ( sender == Last )

{

if ( lstItem . Items . Count > 0 )

{

lstItem . SelectedIndex = lstItem . Items . Count - 1 ;

}

}

//按鈕"上一條"事件處理程序

if ( sender == PRev )

{

if ( lstItem . SelectedIndex > 0 )

{

lstItem . SelectedIndex = lstItem . SelectedIndex - 1 ;

}

}

//按鈕"下一條"事件處理程序

if ( sender == Next )

{

if ( lstItem . SelectedIndex < lstItem . Items . Count - 1 )

{

lstItem . SelectedIndex = lstItem . SelectedIndex + 1 ;

} }

(4).如何實現組件中的指定條目的移位: 

移位包括二種,其一是向上移位,其二是向下移位。程序中具體的實現思路是:創建一個ListItem對象,并把要移位指定的條目中的內容先暫放在此新建的這個對象中。假如選定的是向上移位,就把當前選定的條目的上一個條目的值賦值給當前選定的條目,然后把剛才新建的對象的值,再賦值給選定條目的上一個條目,完成條目的向上移位操作。對于向下移位,可以仿效上面的做法,但和上面做法的主要區別在于不是選定條目的上一個條目了,而是選定條目的下一個條目。下列語句就是就是實現這種思路的具體的程序代碼:


//按鈕"向上移位"和"向下移位"事件處理程序

if ( ( sender == Up && lstItem . SelectedIndex > 0 ) ( sender == Down && lstItem . SelectedIndex < lstItem . Items . Count - 1 ) )

{

int offset ;

if ( sender == Up )

{

offset = -1 ;

}

else

{

offset = 1 ;

}

ListItem lstTemp = new ListItem ( lstItem . SelectedItem . Text , lstItem . SelectedItem . Value ) ;

lstItem . Items [ lstItem.SelectedIndex ] .Text = lstItem . Items [ lstItem . SelectedIndex + offset ] . Text ;

lstItem . Items [ lstItem . SelectedIndex ] . Value = lstItem . Items [ lstItem . SelectedIndex + offset ] . Value ;

lstItem . Items [ lstItem . SelectedIndex + offset ] . Text = lstTemp . Text ;

lstItem . Items [ lstItem . SelectedIndex + offset ] . Value = lstTemp . Value ;

lstItem . SelectedIndex = lstItem . SelectedIndex + offset ;

}  ListBox組件是一個程序設計中經常使用到的組件,在Visual C#和Visual Basic .Net程序中使用這個組件,必須要在程序中導入.Net FrameWork SDK中名稱空間System.Windows.Forms,因為在System.Windows.Forms名稱空間中定義了這個組件。在ASP.NET的Web頁面中,ListBox組件是作為一個服務器端組件的形式出現的,所謂服務器端組件就是這些組件是在服務器端存在的。本文就是來介紹ListBox組件在ASP.NET的Web頁面中的具體使用和操作方法。

一. 如何在ASP.NET頁面中定義一個ListBox組件: 

在ASP.NET頁面中創建一個ListBox組件的語法如下:

<asp:ListBox Id = "MyListBox" runat = "server" >

<asp:ListItem Value = "1" >第一個條目</asp:ListItem >

<asp:ListItem Value = "2" >第二個條目</asp:ListItem >

注釋:這里還可以加入類似上面的若干條目

.....

</asp:ListBox >

在Web頁面中執行上面的語句就可以產生一個名稱為"MyListBox",包含若干條目的ListBox組件。

二. ListBox組件中常用的屬性:

我們通過以下表格來說明ListBox組件的一些常用的屬性:

屬性名稱 屬性代表的意義 SelectionMode 組件中條目的選擇的類型即:多選、單選。Single,Multiple Rows 此組件顯示總共多少行 Selected 檢測條目十分被選中 SelectedItem 返回的類型是ListItem,獲得組件中被選擇的條目 Count 組件中條目的總數 SelectedIndex 組件中被選擇的條目的索引值 Items 泛指組件中所有的條目,每一個條目的類型都是ListItem

三. 通過一個例子來把握ListBox組件在ASP.NET頁面中的具體用法:

在下面介紹ListBox組件在ASP.NET中的使用方法的時候,程序采用的程序設計語言是Visual C#。

(1).如何在ListBox組件添加新的條目:

通過以下語句就可以在名稱為lstItem的ListBox組件中增加一個名稱為"Sample"的條目:

lstItem . Items . Add ( new ListItem ( "Sample" ) ) 

(2).如何在ListBox組件中刪除指定的條目:

下列語句就是刪除名稱為lstItem的ListBox組件中的選定的一個條目:

  lstItem . Items . Remove ( lstItem . SelectedItem )

(3).如何在組件中移動指向條目的指針: 

移動條目的指針主要有四種方式:至首條目、至尾條目、下一條、上一條。在程序設計中主要是通過操作組件的Count和SelectedIndex屬性來實現以上四種方式的。以下就是具體實現這四種方式的程序代碼:

//按鈕"至首條"事件處理程序

if ( sender == First )

{

if ( lstItem . Items . Count > 0 )

{

lstItem . SelectedIndex = 0 ;

}

}

//按鈕"至尾條"事件處理程序

if ( sender == Last )

{

if ( lstItem . Items . Count > 0 )

{

lstItem . SelectedIndex = lstItem . Items . Count - 1 ;

}

}

//按鈕"上一條"事件處理程序

if ( sender == Prev )

{

if ( lstItem . SelectedIndex > 0 )

{

lstItem . SelectedIndex = lstItem . SelectedIndex - 1 ;

}

}

//按鈕"下一條"事件處理程序

if ( sender == Next )

{

if ( lstItem . SelectedIndex < lstItem . Items . Count - 1 )

{

lstItem . SelectedIndex = lstItem . SelectedIndex + 1 ;

} }

(4).如何實現組件中的指定條目的移位: 

移位包括二種,其一是向上移位,其二是向下移位。程序中具體的實現思路是:創建一個ListItem對象,并把要移位指定的條目中的內容先暫放在此新建的這個對象中。假如選定的是向上移位,就把當前選定的條目的上一個條目的值賦值給當前選定的條目,然后把剛才新建的對象的值,再賦值給選定條目的上一個條目,完成條目的向上移位操作。對于向下移位,可以仿效上面的做法,但和上面做法的主要區別在于不是選定條目的上一個條目了,而是選定條目的下一個條目。下列語句就是就是實現這種思路的具體的程序代碼:


//按鈕"向上移位"和"向下移位"事件處理程序

if ( ( sender == Up && lstItem . SelectedIndex > 0 ) ( sender == Down && lstItem . SelectedIndex < lstItem . Items . Count - 1 ) )

{

int offset ;

if ( sender == Up )

{

offset = -1 ;

}

else

{

offset = 1 ;

}

ListItem lstTemp = new ListItem ( lstItem . SelectedItem . Text , lstItem . SelectedItem . Value ) ;

lstItem . Items [ lstItem.SelectedIndex ] .Text = lstItem . Items [ lstItem . SelectedIndex + offset ] . Text ;

lstItem . Items [ lstItem . SelectedIndex ] . Value = lstItem . Items [ lstItem . SelectedIndex + offset ] . Value ;

lstItem . Items [ lstItem . SelectedIndex + offset ] . Text = lstTemp . Text ;

lstItem . Items [ lstItem . SelectedIndex + offset ] . Value = lstTemp . Value ;

lstItem . SelectedIndex = lstItem . SelectedIndex + offset ;

} QQread.com 推出游戲功略 http://www.qqread.com/netgame/game/index.Html 魔獸世界 跑跑卡丁車 街頭籃球 水滸Q傳 龍與地下城OL 征服  軒轅劍5 FIFA07 熱血江湖 大唐風云 夢幻西游 武林外傳

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 盐城市| 驻马店市| 广元市| 伊吾县| 海原县| 兴义市| 普安县| 南陵县| 肃宁县| 多伦县| 东海县| 阜阳市| 尉犁县| 黔江区| 江都市| 波密县| 土默特右旗| 通州区| 万安县| 北碚区| 柯坪县| 辽阳市| 曲水县| 永登县| 色达县| 蒲城县| 安阳市| 平乐县| 化州市| 隆回县| 贵州省| 丽水市| 中西区| 筠连县| 金寨县| 沂水县| 珠海市| 泌阳县| 武陟县| 河东区| 河源市|