C#listbox用法

. 属性列表:

    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,new   ListItem("插入在第3行之后项",""));

this.ListBox1.Items.Insertat(index,ListItem)

ListBox1.Items.Insert(0,new   ListItem("text","value"));

参考博客:http://hi.baidu.com/sddyszb/blog/item/3fc13bc0aea260110ff4777b.html

转载于:https://www.cnblogs.com/firstWolf/archive/2012/05/02/ListBox.html

ListBoxC# 中的一个常用控件,它可以用来显示一个列表,用户可以通过 ListBox 来选择一个或多个项目。下面是 ListBox用法详解: 1. 添加 ListBox 控件到窗体中 将 ListBox 控件从工具箱中拖拽到窗体中即可。 2. 设置 ListBox 的属性 常用的属性有: - Items:ListBox 中的项目集合。 - SelectionMode:选择模式,可以是 Single(单选)、MultiSimple(多选,按下 Ctrl 键)、MultiExtended(多选,可以使用鼠标拖动)。 - DisplayMember:指定 ListBox 显示项目时所使用的属性。 - ValueMember:指定 ListBox 存储项目时所使用的属性。 3. 添加项目到 ListBox 中 可以通过代码或者设计器来添加项目。 通过代码添加项目: ```csharp listBox1.Items.Add("项目1"); listBox1.Items.Add("项目2"); listBox1.Items.Add("项目3"); ``` 通过设计器添加项目: 在 ListBox 的属性窗口中找到 Items 属性,点击右侧的“…”按钮,在弹出的窗口中添加项目。 4. 获取选中的项目 可以通过 SelectedItem 属性获取当前选中的项目,也可以通过 SelectedItems 属性获取所有选中的项目。 ```csharp // 获取当前选中的项目 string selectedItem = listBox1.SelectedItem.ToString(); // 获取所有选中的项目 List<string> selectedItems = new List<string>(); foreach (var item in listBox1.SelectedItems) { selectedItems.Add(item.ToString()); } ``` 以上就是 ListBox 控件的用法详解。需要注意的是,ListBox 中的项目可以是任何类型的对象,不仅仅是字符串。如果需要在 ListBox 中显示自定义类型的对象,需要通过 DisplayMember 和 ValueMember 属性来指定显示和存储的属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值