志在指尖
用双手敲打未来

listbox(vs中listbox控件用法详解)

listbox

C#中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;
//将当时条目的文本以及值都保存到一个暂时变量里边
ListItemlt=newListItem(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”));listbox

vs中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;
//将当时条目的文本以及值都保存到一个暂时变量里面
ListItemlt=newListItem(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;
出自51CTO.COM博客
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.详细代码如下:
//读取右边选中项目
ArrayListarrRight=newArrayList();
foreach(ListItemiteminthis.ListBoxRight.Items)//按类型listitem读取listbox中选定项
{
if(item.Selected)//判别是否选中
{
arrRight.Add(item);
}
}
//右边移除选定项目左边增加
foreach(ListItemiteminarrRight)
{
this.ListBoxLeft.Items.Add(item);
this.ListBoxRight.Items.Remove(item);
}
不能将item的增加删去直接写在if(item.Selected){}内,因为项目remove后会出现错误

未经允许不得转载:IT技术网站 » listbox(vs中listbox控件用法详解)
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载