實現效果:
知識運用:
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+"項被選中"); //彈出對話框 }