checkbox ListView that will only allow one checked item

  • Thread starter Thread starter sklett
  • Start date Start date
S

sklett

I wasn't sure how to deal with this, but this is what I cam up with. If
possible, I'd like a little feedback if there is a better way to deal with a
situation where changes in an event handler cause the event to fire.

private void listView_AvailableTests_ItemChecked(object sender,
ItemCheckedEventArgs e)
{
listView_AvailableTests.ItemChecked -=
listView_AvailableTests_ItemChecked;

foreach(ListViewItem item in listView_AvailableTests.Items)
{
if(item != e.Item)
{
item.Checked = false;
}
}

listView_AvailableTests.ItemChecked +=
listView_AvailableTests_ItemChecked;
}

The above code works, but it just seems a little "odd" to me for some reason
;0)

Any and all comments are welcome.

Thanks,
Steve
 
Another option:

Have a form level variable of type ListViewItem and set it to null:
private ListViewItem _curItem = null;

In the ItemChecked event, have:
if (_curItem != null)
{
_curItem.Checked = false;
}
_curItem = e.Item;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top