piston2033
piston2033

Reputation: 15

List View Item.Selected = false cause all the checkboxes unchecked

There is a problem with c#. I have a listview with checkboxes and 4 items in it.

I want the item selected, if the user check its checkbox and deselected if the user uncheck its checkbox. The problem is, when I uncheck a checkbox, the others are beeing unchecked at the same time.

If I change the multiselect property to false, checkboxes work as they should be but i want to select more than one item.

private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
{
    foreach(ListViewItem lvi in listView1.Items)
    {
        if(lvi.Checked == true)
        {
            lvi.Selected = true;
        }
        else
        {
            lvi.Selected = false;
        }
    }
}

Upvotes: 1

Views: 1766

Answers (1)

JP-Hundhausen
JP-Hundhausen

Reputation: 80

I had a similar Problem. I synced the row select and the checkbox.

Here is the code:

private void ListView1_ItemChecked(object sender, ItemCheckedEventArgs e)
{
   for (int i = 0; i < listView1.Items.Count; i++)
   {
      listView1.ItemSelectionChanged -= ListView1_ItemSelectionChanged;
      listView1.ItemCheck -= ListView1_ItemCheck;
      listView1.Items[i].Selected = listView1.Items[i].Checked;
      listView1.ItemSelectionChanged += ListView1_ItemSelectionChanged;
      listView1.ItemCheck += ListView1_ItemCheck;
   }
 }

private void ListView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
   for (int i = 0; i < listView1.Items.Count; i++)
   {
      listView1.ItemChecked -= ListView1_ItemChecked;
      listView1.ItemCheck -= ListView1_ItemCheck;
      listView1.Items[i].Checked = listView1.Items[i].Selected;
      listView1.ItemChecked += ListView1_ItemChecked;
      listView1.ItemCheck += ListView1_ItemCheck;
   }
 }

private void ListView1_ItemCheck(object sender, ItemCheckEventArgs e)
{
   if (e.NewValue != CheckState.Unchecked) return;
   Point locaPoint = listView1.PointToClient(MousePosition);
   ListViewItem prevHoverdItem = listView1.GetItemAt(locaPoint.X, locaPoint.Y);
   if (prevHoverdItem == null) return;
   if (prevHoverdItem != listView1.Items[e.Index]) e.NewValue = CheckState.Checked;
}

Unloading the Events is important, to prevent looping. The ItemCheck Event is the Event that gets called when the Checkboxes get a change and that for every Checkbox. At first we check if a box gets unchecked. We check if the ListViewItem under your Mouse Cursor is that Item that we currently at. When not, we change the NewValue to checked, so it doesn't get unchecked. With that, only the Item we clicked at, gets unchecked. When you are a few Pixels off the Checkbox, than it doesn't work, because thats how the selection in a Listview works. But with that Code, it should be easy to understand how to also change this.

PS.: I hope i didn't messed up the Code, because i copied it out of a Project with more Code in it and quickly changed the names.

Upvotes: 1

Related Questions