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方面编程的首要条件,也是一个比较重要的环节。