WinForm中的ListBox组件编程
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 <asp:ListItem Value = "2" 注释:这里还可以加入类似上面的若干条目 ..... </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 . SelectedIndex = 0 ; } } //按钮"至尾条"事件处理程序 if ( sender == Last ) { if ( lstItem . Items . Count > 0 ) { lstItem . } } //按钮"上一条"事件处理程序 if ( sender == Prev ) { if ( { lstItem . SelectedIndex = } } //按钮"下一条"事件处理程序 if ( sender == Next ) { if ( lstItem . SelectedIndex < { lstItem . SelectedIndex = } } |
(4).如何实现组件中的指定条目的移位:
移位包括二种,其一是向上移位,其二是向下移位。程序中具体的实现思路是:创建一个ListItem对象,并把要移位指定的条目中的内容先暂放在此新建的这个对象中。如果选定的是向上移位,就把当前选定的条目的上一个条目的值赋值给当前选定的条目,然后把刚才新建的对象的值,再赋值给选定条目的上一个条目,完成条目的向上移位操作。对于向下移位,可以仿效上面的做法,但和上面做法的主要区别在于不是选定条目的上一个条目了,而是选定条目的下一个条目。下列语句就是就是实现这种思路的具体的程序代码:
|
//按钮"向上移位"和"向下移位"事件处理程序 if ( ( sender == Up && lstItem . { int offset ; if ( sender == Up ) { offset = -1 ; } else { offset = 1 ; } ListItem lstTemp = lstItem . Items [ lstItem.SelectedIndex ] .Text = lstItem . Items [ lstItem . lstItem . Items [ lstItem . SelectedIndex + offset ] . Text lstItem . Items [ lstItem . SelectedIndex + lstItem . SelectedIndex = } |
四. 本文中源程序代码(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 ( { lstItem . Items . Add ( new ListItem ( } } //按钮"删除"事件处理程序 if ( { if ( lstItem . SelectedIndex > -1 ) { lstItem . Items . Remove ( lstItem . SelectedItem ) ; } } //按钮"向上移位"和"向下移位"事件处理程序 if ( ( sender == Up && { int offset ; if ( sender == Up ) { offset = -1 ; } else { offset = 1 ; } ListItem lstTemp = lstItem . Items [ lstItem.SelectedIndex ] .Text = lstItem . Items [ lstItem . Items [ lstItem . lstItem . Items [ lstItem . SelectedIndex + offset ] . Text = lstItem . Items [ lstItem . SelectedIndex + offset ] . lstItem . SelectedIndex = lstItem . } //按钮"至首条"事件处理程序 if ( sender { if ( lstItem . Items . Count > 0 ) { lstItem . SelectedIndex = 0 ; } } //按钮"至尾条"事件处理程序 if ( sender == Last ) { if ( { lstItem . SelectedIndex = } } //按钮"上一条"事件处理程序 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 = <tr > <td > 请输入要增加的条目名称:</td > <td > <asp:TextBox id = "txtItem" TextMode = "SingleLine" runat = "server"/> <asp:Button id = Add Text = "增加条目" runat = "server" onclick = </td > </tr > <tr > <td <asp:ListBox id = "lstItem" Width = 200 Height = 250 <asp:ListItem > 第一个条目 </asp:ListItem > </asp:ListBox > </td > <td > <asp:Button id = <asp:Button id = "Up" Text = "向上移位" runat = "server" onclick = <asp:Button id = "Down" Text = "向下移位" runat = "server" <asp:Button id = "First" Text = "至首条" runat <asp:Button id="Prev" Text = <asp:Button id = <asp:Button id = "Last" Text = "至尾条" runat = "server" onclick = </td > </tr > </table > </form <body > </html > |
WinForm中的ListBox组件编程的更多相关文章
- winform中的ListBox和ComboBox绑定数据
将集合数据绑定到ListBox和ComboBox控件,界面上显示某个属性的内容 //... //自定义了Person类(有Name,Age,Heigth等属性) List<Person> ...
- WinForm中获取Listbox、DataGridView等控件某行对应的数据
Listbox:listbox.SelectedItem as XXX DataGridView:dataGridView1.Rows[i].Cells[1].Value.ToString()
- [Winform]Media Player com组件应用中遇到的问题
摘要 最近一个项目中,需要用到在客户端全屏循环播放视频,当时考虑使用开源的播放器,但控制起来不方便,然后考虑既然都是windows系统,那么可以考虑使用微软自带的Media Player播放器.所以在 ...
- 在C#中实现listbox的项上下移动(winform) 标准
在C#中实现listbox的项上下移动(winform) 收藏人:梅毛子360 2013-10-02 | 阅:1 转:2 | 分享 | 来源 usi ...
- 浅析C#组件编程中的一些小细节
控件与组件的区别(Control&Component的区别) 作者:作者不详 发布日期:2011-06-30 12:08:41 控件与组件的区别(Control&Component的 ...
- 分享在winform下实现模块化插件编程-优化版
上一篇<分享在winform下实现模块化插件编程>已经实现了模块化编程,但我认为不够完美,存在以下几个问题: 1.IAppContext中的CreatePlugInForm方法只能依据完整 ...
- .Net中的反应式编程(Reactive Programming)
系列主题:基于消息的软件架构模型演变 一.反应式编程(Reactive Programming) 1.什么是反应式编程:反应式编程(Reactive programming)简称Rx,他是一个使用LI ...
- Net中的反应式编程
Net中的反应式编程(Reactive Programming) 系列主题:基于消息的软件架构模型演变 一.反应式编程(Reactive Programming) 1.什么是反应式编程:反应式编程 ...
- .NET中使用APlayer组件自制播放器
目录 说明 APlayer介绍 APlayer具备功能 APlayer使用 自制播放器Demo 未完成工作 源码下载 说明 由于需求原因,需要在项目中(桌面程序)集成一个在线播放视频的功能.大概要具备 ...
随机推荐
- React Native导航器Navigator
React Native导航器Navigator 使用导航器可以让你在应用的不同场景(页面)间进行切换.导航器通过路由对象来分辨不同的场景.利用renderScene方法,导航栏可以根据指定的路由来渲 ...
- 170925_1 Python socket 创建TCP的服务器端和客户端
[Python版本]3.6 [遇到的问题] 客户端和服务器端都遇到:TypeError: a bytes-like object is required, not 'str' [解决方案] 参考:ht ...
- Centos6.6 安装rsync服务端
一.介绍 在工作中经常遇到代码分发,或者是资料备份,都会用到rsync,配置不算复杂,仅做下记录,安装环境如下: 1) Centos6.6 2) rsync-3.0.6-12.el6.x86_64 3 ...
- Memcached 之PHP实现服务器集群一致性hash算法
/** * memcached 一致性hash,分布式算法 * Class MemcacheCluster */ class MemcacheCluster { protected $nodes = ...
- php 在不知道字符串有多长的情况下,如何去除前三个字符?
$string='字符串';$subject=substr_replace(string,'',0,3);
- 小白年薪24万,为什么Linux运维工程师薪资这么高?
借了云计算的东风,Linux岗位这几年是越来越火,特别是Linux云计算运维工程师,如今早已成为互联网的核心岗位之一,薪资待遇飞快的上涨. 作为一个细分的专业岗位,Linux云计算工程师由于其入门学习 ...
- js 简单小知识
1. javascript的typeof返回哪些数据类型: string, boolean, number, undefined, function, object 2. split() join() ...
- SQL With As 用法Sql 四大排名函数(ROW_NUMBER、RANK、DENSE_RANK、NTILE)简介
Sql 四大排名函数(ROW_NUMBER.RANK.DENSE_RANK.NTILE)简介 排名函数是Sql Server2005新增的功能,下面简单介绍一下他们各自的用法和区别.我们新建一张O ...
- 8.2.3 覆写 Equals
经过对四种不同类型判等方法的讨论,我们不难发现不管是 Equals 静态方法.Equals 虚方法 抑或==操作符的执行结果,都可能受到覆写 Equals 方法的影响.因此研究对象判等就必须将注意 力 ...
- [HDU5807] Keep In Touch
\(Keep\ In\ Touch\):保持联络 \(Informatik\ verbindet\ dich\ und\ mich.\) 信息将你我连结? 发现这个方程很容易列出来. \(f[i][j ...