C#ListView控件添加复选框并获取选中的数目

时间:2021-08-09 12:36:30

1、添加复选框:listView1.CheckBoxes = true;

2、选中listview并获取选中的数目:

        private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
{
e.Item.Selected = e.Item.Checked;
m = listView1.CheckedItems.Count;
label1.Text = "当前选中数:" + m.ToString();
}

说明:点击复选框或者双击listview均可选中某一行,但是跟comboBox控件不一样的是,获取选中数目要用:m = listView1.CheckedItems.Count;而不是listView1.SelectedItems.Count; !!!!!!!!!

3、获取listview中复选框中选定的多个值

            int k = 0;
int m = listView1.CheckedItems.Count;
string[] a = new string[m];
Queue<string> Q = new Queue<string>();

for (int i = 0; i < m; i++)
if (listView1.CheckedItems[i].Checked)
Q.Enqueue(listView1.CheckedItems[i].SubItems[1].Text);
while (Q.Count > 0)
{
a[k] = Q.Dequeue();
k++;
}