選中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+"項被選中"); //彈出對話框
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM