4

I am making quarantine and I have listView for showcase of all viruses, and I added checkbox to listview in Header Column (I don't know if I can add checkbox in header column). I added separate column with checkboxes.

I want that checkbox in header when it's checked that all checkboxes in viewlist items are checked.

I hope somebody can help. Thanks!

Vepth
  • 665
  • 4
  • 20
  • 2
    Hook the OnClick or Changed event of the checkbox, and loop through the listview items, setting the checkbox in each item to checked. – Robert Harvey Jan 23 '19 at 23:39
  • [How to Add a Checkbox to a List View Column Header in c# Winform App?](https://stackoverflow.com/a/35790015/3110834) – Reza Aghaei Jan 24 '19 at 07:53

2 Answers2

4

Using a Button Click:

private void button1_Click(object sender, EventArgs e)
{ 
   for (int i = 0; i < listView1.Items.Count; i++)
  {
    listView1.Items[i].Checked = true;
  }
}

Clicking on the Column Header you can use the listView1_ColumnClick(object sender, ColumnClickEventArgs e) event.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
3

In case you have a separate "Select All" checkbox for selecting all items you can have code like:

private void cbSelectAll_CheckedChanged(object sender, EventArgs e)
{
    foreach (ListViewItem listViewItem in listView.Items)
    {
        listViewItem.Checked = cbSelectAll.Checked;
    }
}

If you want to check all items by clicking on any of list view item, then you have to subscribe to ItemChecked event of a list view:

private void listView_ItemChecked(object sender, ItemCheckedEventArgs e)
{
    foreach (ListViewItem listViewItem in listView.Items)
    {
        listViewItem.Checked = e.Item.Checked;
    }
}

PS: To display checkbox above list items set the CheckBoxes flag to true

opewix
  • 4,993
  • 1
  • 20
  • 42