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组件编程的更多相关文章

  1. winform中的ListBox和ComboBox绑定数据

    将集合数据绑定到ListBox和ComboBox控件,界面上显示某个属性的内容 //... //自定义了Person类(有Name,Age,Heigth等属性) List<Person> ...

  2. WinForm中获取Listbox、DataGridView等控件某行对应的数据

    Listbox:listbox.SelectedItem as XXX DataGridView:dataGridView1.Rows[i].Cells[1].Value.ToString()

  3. [Winform]Media Player com组件应用中遇到的问题

    摘要 最近一个项目中,需要用到在客户端全屏循环播放视频,当时考虑使用开源的播放器,但控制起来不方便,然后考虑既然都是windows系统,那么可以考虑使用微软自带的Media Player播放器.所以在 ...

  4. 在C#中实现listbox的项上下移动(winform) 标准

      在C#中实现listbox的项上下移动(winform) 收藏人:梅毛子360   2013-10-02 | 阅:1  转:2  |  分享    |    来源              usi ...

  5. 浅析C#组件编程中的一些小细节

    控件与组件的区别(Control&Component的区别) 作者:作者不详  发布日期:2011-06-30 12:08:41 控件与组件的区别(Control&Component的 ...

  6. 分享在winform下实现模块化插件编程-优化版

    上一篇<分享在winform下实现模块化插件编程>已经实现了模块化编程,但我认为不够完美,存在以下几个问题: 1.IAppContext中的CreatePlugInForm方法只能依据完整 ...

  7. .Net中的反应式编程(Reactive Programming)

    系列主题:基于消息的软件架构模型演变 一.反应式编程(Reactive Programming) 1.什么是反应式编程:反应式编程(Reactive programming)简称Rx,他是一个使用LI ...

  8. Net中的反应式编程

    Net中的反应式编程(Reactive Programming)   系列主题:基于消息的软件架构模型演变 一.反应式编程(Reactive Programming) 1.什么是反应式编程:反应式编程 ...

  9. .NET中使用APlayer组件自制播放器

    目录 说明 APlayer介绍 APlayer具备功能 APlayer使用 自制播放器Demo 未完成工作 源码下载 说明 由于需求原因,需要在项目中(桌面程序)集成一个在线播放视频的功能.大概要具备 ...

随机推荐

  1. JS——offset

    1.offsetWidth.offsetHeight返回盒子宽度和高度,包括padding与border,不包括margin 2.offsetLeft.offsetTop返回盒子距离定位盒子的x轴方向 ...

  2. Python开发工具搭建-Pycharm

    PyCharm2017. 3.X专业版 安装使用. 注册码激活 本文以 Windows系统 为例: 1.开发工具获取及下载 Anaconda(Python 的集成工具 ) 下载地址: https:// ...

  3. nagios 插件ndoutils 安装配置

    nagios 插件ndoutils 安装配置 原文地址:http://www.cnblogs.com/caoguo/p/5022645.html # Nagios install ndoutils # ...

  4. 阿里P8架构师详解Java性能调优策略

    一.性能测试 Ⅰ.测试方法 微基准性能测试 可以精准定位到某个模块或者某个方法的性能问题,例如对比一个方法使用同步实现和非同步实现的性能差异 宏基准性能测试 宏基准性能测试是一个综合测试,需要考虑到测 ...

  5. Java对象的创建及使用

    Java对象的创建及使用 对象是类的具体实例(instance),是真实存在的个体:

  6. 最适合初学者的Linux运维学习教程2018版

    Linux运维工程师是一个新颖岗位,现在非常吃香,目前从行业的角度分析,随着国内软件行业不断发展壮大,越来越多复杂系统应运而生,为了保证系统稳定运行,必须要有足够多的Linux运维工程师.维护是软件生 ...

  7. P4047 [JSOI2010]部落划分(最小生成树)

    题目描述 聪聪研究发现,荒岛野人总是过着群居的生活,但是,并不是整个荒岛上的所有野人都属于同一个部落,野人们总是拉帮结派形成属于自己的部落,不同的部落之间则经常发生争斗.只是,这一切都成为谜团了——聪 ...

  8. SOUI界面库 添加 windows系统文件图标皮肤

    最近在学习soui界面库.其中有用到SListCtrl这个控件来现在文件信息.控件用法基本上和mfc 的CListCtrl差不多.也支持图标显示.但是图标是要自己加入图标图片的.这个就有点不好弄.于是 ...

  9. [luogu4054 JSOI2009] 计数问题(2D BIT)

    传送门 Solution 2D BIT模板 Code //By Menteur_Hxy #include <cmath> #include <cstdio> #include ...

  10. BZOJ 2038 2009国家集训队 小Z的袜子【模板·莫队】

    [题解] 1,先说说莫队算法. 莫队算法是用来离线处理区间问题的算法.非常易于理解和使用,且运用十分广泛. 假设我们现在已知区间[L,R]的答案,如果我们能以较低的时间复杂度扩展得到区间$[L-1,R ...