简体   繁体   English

如何只检查checkedlistbox中的一项

[英]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(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...但问题是,当我一次又一次地单击所选项目时,不应取消选中该所选项目,这意味着应始终选中至少一项...

I agree with commentators above - you should consider using radiobuttons.我同意上面的评论员 - 您应该考虑使用单选按钮。 But if you really need CheckedListBox, then use this ItemChecked event handler instead:但是,如果您确实需要 CheckedListBox,请改用此 ItemChecked 事件处理程序:

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.我无法让上述代码在checkedListBox1_ItemCheck 中工作。 I had to modify a portion of it ans include it in the checkedListBox1_SelectedIndexChanged event.我不得不修改它的一部分并将它包含在checkedListBox1_SelectedIndexChanged 事件中。 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.基本上,如果您选中了 1 个以上的框,请将最后一个切换为新的。 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,如果您的目的是始终只选择一个项目,请将其用作MouseUp事件,

    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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM