Wednesday, February 23, 2011

CheckedListBox Mutual Exclusive Selection

I thought that this would be a simple property setting in the CheckedListBox, but no I guess not. I assume that the individual only wants to select one item, and that item should be both selected and checked.

Here's how I did it:

private void checkOnlySelection(CheckedListBox cbox)
Object selectedItem = cbox.SelectedItem;
if (selectedItem == null)
for (int i = 0; i < cbox.Items.Count; i++)
Object o = cbox.Items[i];
bool match = o == selectedItem;
cbox.SetItemChecked(i, match);

Just call this method in the SelectedIndexChange event handler and it will do the trick.

No comments:

Post a Comment