选中ListBox控件中的全部项

实现效果:

    

知识运用:
  ListBox控件的SelectedItems属性  //获取ListBox控件中被选中数据项的集合

  public ListBox.SelectedObjectCollection SelectedItems{get;}

  和SetSelected方法    //选择或清除控件中选定的数据项

  public void SetSelected(int index,bool value)

  属性值:  index:整型数值,ListBox控件中要选择或清除对其选定项的从零开始的索引

        value:布尔值,选择指定项设为true 否则值为false

实现代码:

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.SelectionMode = SelectionMode.MultiExtended;   //可以选多项
            for (int i = 0; i < listBox1.Items.Count; i++)          //遍历数据项集合
            {
                listBox1.SetSelected(i, true);                      //选定数据项
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show(listBox1.SelectedItems.Count+"项被选中"); //弹出对话框
        }

猜你喜欢

转载自www.cnblogs.com/feiyucha/p/10159798.html