【问题标题】:CheckedListBox Only One Checked But Keep LastCheckedListBox 仅选中一个但保留最后一个
【发布时间】:2014-07-03 21:30:36
【问题描述】:

我很难让 CheckedListBox 以我想要的方式运行。我想要完成的是获得一个 CheckedListBox 并在 PageLoad 上选中第一个框。我只想在任何给定时间选中一个复选框,但我也不希望用户取消选中最后一个复选框。我可以做一个或另一个,但我似乎不能同时做。

这是我用来完成只选中一个复选框的任务的一些代码 sn-ps。这些问题是在没有选中复选框的情况下可以取消选中最后一个选择。

第一个片段:

        if(e.NewValue == CheckState.Checked)
        {
            // Uncheck the other items
            for (int i = 0; i < defCheckedListBox.Items.Count; i++)
            {
                if (e.Index != i)
                {
                    this.defCheckedListBox.SetItemChecked(i, false);
                }
            }
        }

第二个片段

        // Ensure that we are checking an item
        if (e.NewValue != CheckState.Checked)
        {
            return;
        }

        // Get the items that are selected
        CheckedListBox.CheckedIndexCollection selectedItems = this.defCheckedListBox.CheckedIndices;

        // Check that we have at least 1 item selected
        if (selectedItems.Count > 0)
        {
            // Uncheck the other item
            this.defCheckedListBox.SetItemChecked(selectedItems[0], false);
        }

这是我用来防止最后一个复选框“未选中”的方法

        if (laborLevelDefCheckedListBox.CheckedItems.Count == 1)
        {
            if (e.CurrentValue == CheckState.Checked)
            {
                e.NewValue = CheckState.Checked;
            }
        }

我知道这一定很简单,但我认为因为我已经度过了漫长的一周,而且我已经看了太久了,所以它只是不适合我。对此的任何帮助都非常感谢!如果我在周末解决这个问题,我一定会发布我的解决方案。顺便说一句,美国的人们节日快乐:)

【问题讨论】:

  • 使用 RadioButtons 怎么样?
  • 完全同意。我想使用单选按钮,但出于某种原因需要复选框。

标签: c# checkbox checked checkedlistbox


【解决方案1】:

Chris 在 cmets 中提出了一个很好的观点,这感觉就像您正在重新发明单选按钮,但如果您真的希望它与 CheckedListBox 一起工作,那么您几乎已经发布了您发布的代码。我已经改编了您的第一个片段中的代码,我认为这可以解决问题:

//remove the event handler so when we change the state of other items the event
//isn't fired again.
defCheckedListBox.ItemCheck -= defCheckedListBox_ItemCheck;

if (e.NewValue == CheckState.Checked)
{
    // Uncheck the other items
    for (int i = 0; i < defCheckedListBox.Items.Count; i++)
    {
        if (e.Index != i)
        {
            this.defCheckedListBox.SetItemChecked(i, false);
        }
    }
 }
 else
 {
     //the state was not checked.
     //as only one item can ever be Checked inside the event
     //handler the state of not checked is invalid for us; set the state back to Checked.
     e.NewValue = CheckState.Checked;
 }

 //re-add the event handler 
 defCheckedListBox.ItemCheck += defCheckedListBox_ItemCheck;

基本上,唯一的新部分是else,如果状态未选中,我们会在其中重置状态,以及在手动设置其他项目的状态时删除和重新添加事件以防止它再次触发(这如果您愿意,可以使用全局 bool 处理)。

【讨论】:

  • 这完全解决了这个问题。我确实尝试使用 else 语句,但没有删除和重新添加事件处理程序。我没有考虑到这一点。非常感谢!
【解决方案2】:
// Use CheckBoxList Event
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)  {
    // Stops DeInitialize Event
    checkedListBox1.ItemCheck -= checkedListBox1_ItemCheck;
    // Get the new value old value my be in Indeterminate State
    checkedListBox1.SetItemCheckState(e.Index, e.NewValue);
    // Start ReInitialize Event
    checkedListBox1.ItemCheck += checkedListBox1_ItemCheck;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多