程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> WinForm中的ListBox組件編程

WinForm中的ListBox組件編程

編輯:關於C#

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.aspx)和執行的界面:

下圖是執行了下列源程序代碼(listbox.aspx)後,生成的界面:

listbox.aspx源程序代碼,具體如下:

<% @ Page Language = "C#" %>
<html >
<head >
<script runat = "server" >
protected void Button_Click ( object sender , EventArgs e )
{
//按鈕"增加條目"事件處理程序
if ( sender == Add )
{
if ( txtItem.Text != "" )
{
lstItem . Items . Add ( new ListItem ( txtItem . Text ) ) ;
}
}
//按鈕"刪除"事件處理程序
if ( sender == Del )
{
if ( lstItem . SelectedIndex > -1 )
{
lstItem . Items . Remove ( lstItem . SelectedItem ) ;
}
}
//按鈕"向上移位"和"向下移位"事件處理程序
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 ;
}
//按鈕"至首條"事件處理程序
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 ;
}
}
}
</script >
</head >
<body >
<form runat = "server" >
<table >
<tr > <td Colspan = 2 > <h1 > <font color = "red" > WinForm組件ListBox演示程序 </font > </h1 > </td> </tr >
<tr >
<td > 請輸入要增加的條目名稱:</td >
<td >
<asp:TextBox id = "txtItem" TextMode = "SingleLine" runat = "server"/>  
<asp:Button id = Add Text = "增加條目" runat = "server" onclick = "Button_Click"/>
</td >
</tr >
<tr >
<td >ListBox: <br >
<asp:ListBox id = "lstItem" Width = 200 Height = 250 runat = "server" >
<asp:ListItem > 第一個條目 </asp:ListItem >
</asp:ListBox >
</td >
<td >
<asp:Button id = "Del" Text = "刪除" runat = "server" onclick = "Button_Click" />  
<asp:Button id = "Up" Text = "向上移位" runat = "server" onclick = "Button_Click" />  
<asp:Button id = "Down" Text = "向下移位" runat = "server" onclick = "Button_Click" />  
<asp:Button id = "First" Text = "至首條" runat = "server" onclick = "Button_Click" />  
<asp:Button id="Prev" Text = "上一條" runat = "server" onclick = "Button_Click" />  
<asp:Button id = "Next" Text = "下一條" runat = "server" onclick = "Button_Click" />  
<asp:Button id = "Last" Text = "至尾條" runat = "server" onclick = "Button_Click" />  
</td >
</tr >
</table >
</form >
<body >
</html >

五. 總結:

本文主要介紹了WinForm組件中的一個比較重要的組件--ListBox的屬性,以及其的主要的使用方法,掌握這些WinForm組件編程是進行.Net方面編程的首要條件,也是一個比較重要的環節。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved