listbox控件的一些操作
1. 属性列表:
SelectionMode 组件中条目的选择类型,即多选(Multiple)、单选(Single)
Rows 列表框中显示总共多少行
Selected 检测条目是否被选中
SelectedItem 返回的类型是ListItem,获得列表框中被选择的条目
Count 列表框中条目的总数
SelectedIndex 列表框中被选择项的索引值
Items 泛指列表框中的所有项,每一项的类型都是ListItem
2. 取列表框中被选中的值
ListBox.SelectedValue
3. 动态的添加列表框中的项:
ListBox.Items.Add("所要添加的项");
4. 移出指定项:
//首先判断列表框中的项是否大于0
If(ListBox.Items.Count > 0 )
{
//移出选择的项
ListBox.Items.Remove(ListBox.SelectedItem);
}
5. 清空所有项:
//首先判断列表框中的项是否大于0
If(ListBox.Items.Count > 0 )
{
//清空所有项
ListBox.Items.Clear();
}
6. 列表框可以一次选择多项:
只需设置列表框的属性SelectionMode="Multiple",按Ctrl可以多选
7. 两个列表框联动,即两级联动菜单
//判断第一个列表框中被选中的值
switch(ListBox1.SelectValue)
{
//如果是"A",第二个列表框中就添加这些:
case "A"
ListBox2.Items.Clear();
ListBox2.Items.Add("A1");
ListBox2.Items.Add("A2");
ListBox2.Items.Add("A3");
//如果是"B",第二个列表框中就添加这些:
case "B"
ListBox2.Items.Clear();
ListBox2.Items.Add("B1");
ListBox2.Items.Add("B2");
ListBox2.Items.Add("B3");
}
8. 实现列表框中项的移位
即:向上移位、向下移位
具体的思路为:创建一个ListBox对象,并把要移位的项先暂放在这个对象中。
如果是向上移位,就是把当前选定项的的上一项的值赋给当前选定的项,然后
把刚才新加入的对象的值,再附给当前选定项的前一项。
具体代码为:
//定义一个变量,作移位用
index = -1;
//将当前条目的文本以及值都保存到一个临时变量里面
ListItem lt=new ListItem (ListBox.SelectedItem.Text,ListBox.SelectedValue);
//被选中的项的值等于上一条或下一条的值
ListBox.Items[ListBox.SelectedIndex].Text=ListBox.Items[ListBox.SelectedIndex +index].Text;
//被选中的项的值等于上一条或下一条的值
ListBox.Items[ListBox.SelectedIndex].Value=ListBox.Items[ListBox.SelectedIndex+ index].Value;
//把被选中项的前一条或下一条的值用临时变量中的取代
ListBox.Items[ListBox.SelectedIndex].Test=lt.Test;
//把被选中项的前一条或下一条的值用临时变量中的取代
ListBox.Items[ListBox.SelectedIndex].Value=lt.Value;
//把鼠标指针放到移动后的那项上
ListBox.Items[ListBox.SelectedIndex].Value=lt.Value;
9. 移动指针到指定位置:
(1).移至首条
//将被选中项的索引设置为0就OK了
ListBox.SelectIndex=0;
(2).移至尾条
//将被选中项的索引设置为ListBox.Items.Count-1就OK了
ListBox.SelectIndex=ListBox.Items.Count-1;
(3).上一条
//用当前被选中的索引去减1
ListBox.SelectIndex=ListBox.SelectIndex - 1;
(4).下一条
//用当前被选中的索引去加1
ListBox.SelectIndex=ListBox.SelectIndex + 1;
this.ListBox1.Items.Insertat(3,newListItem("插入在第3行之后项",""));
this.ListBox1.Items.Insertat(index,ListItem)
ListBox1.Items.Insert(0,newListItem("text","value"));
ASP.NET中添加控件ListBox , 属性设为 Multiple , 则可进行多选.
就以两个listbox之间多选添加项目为例.
两个控件为listboxleft , listboxright 定义了一个动态数组用于中间存储 arrRight .具体代码如下:
//读取右边选中项目
ArrayList arrRight = new ArrayList();
foreach(ListItem item in this.ListBoxRight.Items) //按类型listitem读取listbox中选定项
{
if(item.Selected) //判断是否选中
{
arrRight.Add(item);
}
}
//右边移除选定项目 左边添加
foreach(ListItem item in arrRight)
{
this.ListBoxLeft.Items.Add(item);
this.ListBoxRight.Items.Remove(item);
}
不能将item的添加删除直接写在if(item.Selected){}内,因为项目remove后会出现错误
Move the Item ofListBox
.aspx
--------------------------------
<asp:ListBoxid="ListBox1" style="Z-INDEX: 105; LEFT: 96px; POSITION:absolute; TOP: 344px" runat="server">
<asp:ListItem Value="a1">a1</asp:ListItem>
<asp:ListItem Value="a2">a2</asp:ListItem>
<asp:ListItem Value="a3">a3</asp:ListItem>
</asp:ListBox>
----------------------------------
.cs
//for个
private voidButton1_Click(object sender, System.EventArgs e){
if (this.ListBox1.SelectedIndex>=0)
{
int i = ListBox1.SelectedIndex;
if(i>0)
{
string values = this.ListBox1.Items[i].Text ;
this.ListBox1.Items[i].Text = this.ListBox1.Items[i-1].Text ;
this.ListBox1.Items[i-1].Text = values;
}
}
}
ASP.NET中ListBox实现DoubleClick事件
2007-06-24 13:18
在ASP.NET中的ListBox控件的使用中很多人都发现没有了WINFORM中ListBox的鼠标双击事件 这样就给我们开发带来很多不方便的地方 也看了很多CSDN上用JS来实现双击事件的方法,都是不完整的,最后发现以下方法是最有效的 首先在WEB页面上加入JS脚本和存放ListBox事件的隐藏输入框 <head> <script language="javascript"> function ListBox1_DoubleClick() { /* we will change value of this hidden field so that in page load event we can identify event. */ document.forms[0].ListBox1Hidden.value = "doubleclicked"; document.forms[0].submit(); } </script> </head> <body> <form runat="server"> <div>Double click on Listbox <br /> <asp:ListBox id="ListBox1" οndblclick="ListBox1_DoubleClick()" runat="server"> <asp:ListItem Value="1">One</asp:ListItem> <asp:ListItem Value="2">Two</asp:ListItem> <asp:ListItem Value="3">Three</asp:ListItem> <asp:ListItem Value="4">Four</asp:ListItem> </asp:ListBox> <input type="hidden" name="ListBox1Hidden" /> </div> <div>click on button <br /> <asp:Button id="Button1" οnclick="Button1_Click" runat="server" Text="Button"/> </div> </form> </body> </html> 最后在WEB窗体加载时候执行下列代码就能实现双击ListBox中Item执行一些操作 void Page_Load(Object sender, EventArgs e){ if(Request.Params["ListBox1Hidden"] != null && (string)Request.Params["ListBox1Hidden"] == "doubleclicked" { //This means It was double click Response.Write("Double Click was fired selected item is " + ListBox1.SelectedItem.Text); //可以在这里加要运行的代码 } } 希望这篇文章能对大家有帮助,不要再受CSDN上帖子的误导了 |
ListBox基本功能首先是列表项的添加,客户端实现代码添加在listbox实例化代码中间,例如:
<asp:ListItem Value="value" Selected=True>Text</asp:ListItem>
若在服务器端实现,为避免每次加载时执行添加列表项,上述代码包含在下面代码中:
if(!IsPostBack)
{
}
WebForm页面上须添加2个listbox(listbox1和lixtbox2)和2个命令按钮,listbox1不为空。列表项从listbox1添加到listbox2须在Button1单击事件中调用Add方法:
ListBox2.Items.Add(ListBox1.SelectedValue);
若要从listbox2中删除列表项的话须在Button2单击事件中调用Remove方法:
ListBox2.Items.Remove(ListBox2.SelectedValue);
列表项从listbox1添加到listbox2后,列表项从listbox1中删除:
int i=0;
while(i<ListBox1.Items.Count)
{
if(ListBox1.Items[i].Selected==true)
{
ListBox2.Items.Add(ListBox1.Items[i]);
ListBox1.Items.Remove(ListBox1.Items[i]);
}
else
i+=1;
}
这样只能实现单项添加,想要实现多项添加,首先设置ListBox1的SelectionMode属性值Multiple,ListBox1允许多项选中。
在Button1单击事件中添加
foreach(ListItem MyItem in ListBox1.Items)
if(MyItem.Selected==true)
ListBox2.Items.Add(MyItem);
想要一次清空ListBox2中所有选项可在Button2单击事件中调用clear方法,
ListBox2.Items.Clear();
若列表项已经添加,不允许二次添加,Button1单击事件中的代码包含在:
if(ListBox2.Items.FindByValue(ListBox1.SelectedValue)==null)
{
}
ListBox与数据库绑定就是指定他的DataSource和DataTextField属性,
ListBox2.DataSource=数据源;
ListBox2.DataTextField="字段名";
ListBox2.DataBind();
如果是在后台做的话
AutoPostBack =true
然后给第一个listbox加事件(SelectedIndexChanged)
然后一个遍历就可以出来了
CODE:
private void ListBox1_SelectedIndexChanged(object sender, System.EventArgse)
{
for(int i=0;i<ListBox1.Items.Count;i++){
ListBox2.Items[i].Selected=ListBox1.Items[i].Selected;
}
}
如果是前台js的话,这里讨论过很多,找找看,思路是一样的。
listbox控件的一些操作相关推荐
- ListBox控件基本操作
ListBox控件单条记录操作 //设置ListBox控件属性: // Styles -> Selection: Single//关联ListBox控件变量: CListBox m_pListR ...
- 『原创』用C++开发WM应用系列(6)——深化ListBox控件
用C++开发WM应用系列文章索引 引言: 在上一篇博文中,我们简单的学习了ListBox控件的项添加.项的全部清除.接上一篇的尾巴,我们将在这篇博文中,介绍一些ListBox的小技巧吧,比如,获取选中 ...
- Win32 ListBox控件
Win32 ListBox控件 创建ListBox控件 创建窗口函数 HWND CrateWindowEx(DWORD dwExStyle , // 窗口的扩展风格,基本没用LPCTSTR lpCla ...
- c# ListBox控件
ListBox控件可以一次呈现多个项,并且语序对控件中的选项进行选择操作,ListBox类公开Items属性,它是一个集合,类型为ListBox.ObjectCollection,是ListBox的一 ...
- html中listbox效果,listbox控件的使用
VB的listbox控件怎么使用 每次我点击一个图片时,怎么让listbox.list加载一个数字 如我点4张 它的假设Listbox控件名字是List1: List1.Clear '清空列表中所有元 ...
- listbox控件用法详解
http://blog.sina.com.cn/s/blog_61e2b6280100svtp.html 1. 属性列表: SelectionMode 组件中条目的选择类型,即多选(Multip ...
- MFC中Listbox控件的简单使用
MFC中listbox控件是为了显示一系列的文本,每个文本占一行. Listbox控件可以设置属性为: LBS_CHILD :(默认)子窗口 LBS_Visible :(默认)可视 LBS_Mul ...
- VFB组件:ListBox控件(列表框)
这是操作系统提供的控件,简单点,就是显示一个列表.系统同时提供强大的自画功能 可以由自己代码画出自己想要的任何东西,比方上面显示的控件属性列表,就是 ListBox控件 常用事件: 常用代码: 进阶用 ...
- java listbox_MFC中Listbox控件的简单使用
MFC中listbox控件是为了显示一系列的文本,每个文本占一行. Listbox控件可以设置属性为: LBS_CHILD :(默认)子窗口 LBS_Visible :(默认)可视 LBS_Mul ...
最新文章
- LeetCode简单题之两个相同字符之间的最长子字符串
- mysql udf编译_MySql UDF 调用外部程序和系统命令
- java实现报表_用存储过程和 JAVA 写报表数据源有什么弊端?
- 04. 二维数组中的查找
- python中node函数的意思_Node.js 函数
- itextSharp 附pdf文件解析
- 机器视觉是什么,未来前景怎么样?
- Linux eclipse clean,小编为你细说eclipse如何clean/clean up重新编译项目
- 史上最好听的十首纯音乐推荐
- 什么是高质量的代码--整理的网上博文
- Django-路由管理 path view
- python爬取微博话题下面的帖子并存入excel文件
- 【研究生】横扫13项中文NLP任务:香侬科技提出汉语字形表征向量Glyce+田字格CNN...
- 《一篇文章全吃透》—YYModel的使用技巧
- 中国青年报:COS 有义务回应公众质疑
- 首次回收重型猎鹰火箭全部助推器,SpaceX再创历史!
- redis cluster如何添加和删除节点?
- python 字典嵌套学习
- 微信小程序地址导航功能实现详解
- C++关系运算和逻辑运算
热门文章
- MSXML2.ServerXMLHTTP HTTPS 证书过期 — msxml3.dll '80072f05'
- python 画天线方向图
- 【硬币计数】基于matlab形态学硬币计数【含Matlab源码 393期】
- getpid()函数和getppid()函数
- 小米手机下载文件失败,提示权限不足
- Vue3 vue-cli、create-react-app、vite 创建vue/react项目(笔记)
- 解决Origin下载慢
- 如何成为云计算解决方案架构师
- 2个字节秒解智能吓数1668加密狗
- 解决:windows10网络图标被重名名为Network