WinForm中的ListBox组件编程

2016-02-19 17:09 5 1 收藏

给自己一点时间接受自己,爱自己,趁着下午茶的时间来学习图老师推荐的WinForm中的ListBox组件编程,过去的都会过去,迎接崭新的开始,释放更美好的自己。

【 tulaoshi.com - 编程语言 】

  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,MultipleRows 此组件显示总共多少行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组件中的选定的一个条目:

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  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)和执行的界面:

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  下图是执行了下列源程序代码(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方面编程的首要条件,也是一个比较重要的环节。

来源:https://www.tulaoshi.com/n/20160219/1614352.html

延伸阅读
标签: Web开发
这个问题来自论坛提问,对dom稍微了解的话还是比较简单的,只要注册一下事件就可以了。 C#代码如下: using System; using System.ComponentModel; using System.Windows.Forms; namespace WindowsApplication5 ...{ public partial class Form1 : Form ...{ public Form1() ...{ Initializ...
        Socket是网络上运行的两个程序间双向通讯的一端,它既可以接受请求,也可以发送请求,利用它可以较为方便的编写网络上数据的传递。在Java中,有专门的Socket类来处理用户的请求和响应。利用Socket类的方法,就可以实现两台计算机之间的通讯。这里就介绍一下在Java中如何利用Socket进行网络编程。 ...
SQL语言作为关系数据库管理系统中的一种通用的结构查询语言,已经被众多的数据库管理系统所采用,如ORACLE、Sybase、Informix等数据库管理系统,它们都支持SQL 语言。Delphi与使用SQL语言的数据库管理系统兼容,在使用Delphi开发数据库应用程序时,我们可以使用SQL语言编程,支持SQL编程是Delphi的一个重要特征,这也是体现Delphi作为一个...
本文对“组件”这个术语进行定义并提供特别是与组件编程相关的 .NET 框架编程概念的概述。虽然“组件”这个术语有多种含义,但在 .NET 框架中,组件是指实现 System.ComponentModel.IComponent 接口的一个类,或从实现该接口的类中直接或间接导出的类。 如果想要您的组件或控件在其他编程语言中可以使用,您必须以符合公共语言规范 (CLS) 的语...
问题的产生: 我的WinForm程序中有一个用于更新主窗口的工作线程(worker thread),但文档中却提示我不能在多线程中调用这个form(为什么?),而事实上我在调用时程序常常会崩掉。请问如何从多线程中调用form中的方法呢? 解答: 每一个从Control类中派生出来的WinForm类(包括Control类)都是依靠底层Windows消息和一个...

经验教程

258

收藏

21
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部