how to check only one item in checkedlistbox

Posted by Shashi Jaiswal on Stack Overflow See other posts from Stack Overflow or by Shashi Jaiswal
Published on 2011-03-10T08:07:03Z Indexed on 2011/03/10 8:10 UTC
Read the original article Hit count: 331

Filed under:
|

I have check list box control and i want to select only one item at a time and i am currently using this code to do the same.

private void CLSTVariable_ItemCheck(object sender, ItemCheckEventArgs e) { // Local variable int ListIndex;

  CLSTVariable.ItemCheck -= CLSTVariable_ItemCheck;

  for (ListIndex = 0; 
       ListIndex < CLSTVariable.Items.Count; 
       ListIndex++)
  {        
    // Unchecked all items that is not currently selected
    if (CLSTVariable.SelectedIndex != ListIndex)
    {
      // set item as unchecked
      CLSTVariable.SetItemChecked(ListIndex, false);
    } // if
    else
    {
      // set selected item as checked
      CLSTVariable.SetItemChecked(ListIndex, true);
    }
  } // for
  CLSTVariable.ItemCheck += CLSTVariable_ItemCheck;  
}

this code is working fine.

but problem is that when i click again and again on selected item then that selected item should not be unchecked, means at least one item should be checked always...

© Stack Overflow or respective owner

Related posts about c#

Related posts about tchecklistbox