Nick_Dev

I have multiple items on windows CheckedListBox Control form and need to allow only one (1) value to be checked at a time. How can this be accomplished

Thanks,



Re: Windows Forms General Allow only ONE (1) checked item on CheckedListBox Control

OrSol

Add to your class this member:

bool executeItemCheck = true;

and in ItemCheck event of CheckedListBox add this code:

CheckedListBox chLB = (CheckedListBox)sender;

if (executeItemCheck)

{

executeItemCheck = false;

if (chLB.CheckedIndices.Count > 0)

chLB.SetItemChecked(chLB.CheckedIndices[0], false);

executeItemCheck = true;

}






Re: Windows Forms General Allow only ONE (1) checked item on CheckedListBox Control

Vijay Joshi

It will be better to replace checked listbox with simple listbox where user can select only one item (multiselect = false).






Re: Windows Forms General Allow only ONE (1) checked item on CheckedListBox Control

nobugz

I don't quite understand OrSol's code but it looks close. Just uncheck all the other items in the ItemCheck event:

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
for (int ix = 0; ix < checkedListBox1.Items.Count; ++ix)
if (ix != e.Index) checkedListBox1.SetItemChecked(ix, false);
}