开发者

how to check only one item in checkedlistbox

I have a 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(Li开发者_StackOverflowstIndex, 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...


I agree with commentators above - you should consider using radiobuttons. But if you really need CheckedListBox, then use this ItemChecked event handler instead:

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if (checkedListBox1.CheckedItems.Count == 1)
    {
        Boolean isCheckedItemBeingUnchecked = (e.CurrentValue == CheckState.Checked);
        if (isCheckedItemBeingUnchecked)
        {
            e.NewValue = CheckState.Checked;
        }
        else
        {
            Int32 checkedItemIndex = checkedListBox1.CheckedIndices[0];
            checkedListBox1.ItemCheck -= checkedListBox1_ItemCheck;
            checkedListBox1.SetItemChecked(checkedItemIndex, false);
            checkedListBox1.ItemCheck += checkedListBox1_ItemCheck;
        }

        return;
    }
}


Well, it was an answer to me! I couldn't get the above code to work in the checkedListBox1_ItemCheck. I had to modify a portion of it ans include it in the checkedListBox1_SelectedIndexChanged event. But I couldn't remove the original code all together. Here is what I've added...

private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (checkedListBox1.CheckedItems.Count > 1)
        {
            Int32 checkedItemIndex = checkedListBox1.CheckedIndices[0];
            checkedListBox1.ItemCheck -= checkedListBox1_ItemCheck;
            checkedListBox1.SetItemChecked(checkedItemIndex, false);
            checkedListBox1.ItemCheck += checkedListBox1_ItemCheck;
        }
    }

Which is basically, if you have more than 1 box checked, switch the last one for the new one. I'm curious why the original code didn't work. And why it has to be there for my new code to work? Thank you.


I found this code it work so well

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


"at least one item should be checked always"

The current solution (the last one) allows items to be checked off. If your purpose is to select exactly one item at all times, use this as a MouseUp event,

    private void ChklbBatchType_MouseUp(object sender, MouseEventArgs e)
    {
        int index = ((CheckedListBox)sender).SelectedIndex;
        for (int ix = 0; ix < ((CheckedListBox)sender).Items.Count; ++ix)
            if (index != ix) { ((CheckedListBox)sender).SetItemChecked(ix, false);   }
              else ((CheckedListBox)sender).SetItemChecked(ix, true);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜