简体   繁体   English

一个复选框,用于选中/取消选中CheckedListBox中的所有其他复选框(C#winForms)

[英]One checkBox to check/uncheck ALL other checkBoxes in CheckedListBox (C# winForms)

I have a checkedListBox with 5 CheckBoxes, and I want the first one to be "All". 我有一个带有5个CheckBoxes的CheckedListBox,我希望第一个为“ All”。 I wrote down this code but i'm getting an endless loop: 我记下了这段代码,但是却遇到了无尽的循环:

private void chkLstBx_ItemCheck(object sender, ItemCheckEventArgs e)
{
    // ----- Get the name of the CheckBox that's changed: -----
    string selected = chkLstBx.SelectedItem + "";
    // ----- If "All" changed: -----
    if (selected.Equals("All"))
        // ----- to TRUE(from unchecked): -----
        if (("" + (chkLstBx.GetItemCheckState(0))).Equals("Unchecked"))
            for (int i = 1; i < chkLstBx.Items.Count; i++)
                **chkLstBx.SetItemChecked(i, true);**
        else // ----- to FALSE(from checked): -----
            for (int i = 1; i < chkLstBx.Items.Count; i++)
                chkLstBx.SetItemChecked(i, false);
// -----------------------------------------------
// -------------- REST OF CODE HERE --------------
// -----------------------------------------------
}

The Bold line (**) unfortunately Calls "chkLstBx_ItemCheck" again... recursively... resulting in an endless loop where the selected is always "All", still "Unchecked", and i begins at 1 once more. 不幸的是,粗体行(**)再次调用“ chkLstBx_ItemCheck” ...递归...导致无休止的循环,其中所选内容始终为“ All”,仍然为“ Unchecked”,并且我再次从1开始。 How can I solve this? 我该如何解决?

In this code, notice that this uses the first item in the list as the All item. 在此代码中,请注意,这会将列表中的第一个项目用作“ All项目。 This is a good practice but a better one is to keep the reference for the all item and use it to check which item is checked in the event handler. 这是一个好习惯,但更好的做法是保留所有项目的引用,并使用它来检查事件处理程序中检查了哪个项目。

private void chkLstBx_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if (e.Index == 0)
    {
        if (e.NewValue == CheckState.Checked)
            ChangeAllCheckBoxValues(true);
        else
            ChangeAllCheckBoxValues(false);
    }
}

private void ChangeAllCheckBoxValues(bool value)
{
    for (int i = 1; i < chkLstBx.Items.Count; i++)
    {
        chkLstBx.SetItemChecked(i, value);
    }
}

I've tried to look at your code and 2 things that comes to my mind: 我试图查看您的代码和我想到的两件事:

1) The reason being is that the binding for ItemCheck was against the chkListBx and not the specific row, as such when you are changing the item check of a specific row, it would call this method again -- finding what was the selected value and call the method again. 1)原因是ItemCheck的绑定是针对chkListBx的,而不是针对特定行的,因此,当您更改特定行的项目检查时,它将再次调用此方法-查找选定的值并再次调用该方法。 I was able to make it work by changing the binding to 'SelectedIndexChanged' -- although you may need to check if it works with the rest of your code. 我可以通过将绑定更改为“ SelectedIndexChanged”来使其工作-尽管您可能需要检查它是否与其余代码一起工作。

2) When changing the binding into 'SelectedIndexChanged', the behaviour that I got was a little bit of the opposite (ie: the All was still unchecked and all the other checkbox was checked). 2)当将绑定更改为“ SelectedIndexChanged”时,我得到的行为与之相反(即:All仍未选中,而所有其他复选框均已选中)。 The reason being was that when it was previously unchecked -- when I clicked it, it set the value to 'Checked' -- and thus when it was being evaluated - the value was considered check -- and it goes to 'unselect' all. 原因是当它以前未被选中时(当我单击它时,它会将值设置为“已检查”),因此,在对其进行评估时,该值被视为“检查”了,然后全部变为“取消选择” 。

NB: On a side note, its better to always have the braces even for a single line 注意:顺便说一句,即使单行也要始终保持括号是更好的选择

for (int i = 1; i < chkLstBx.Items.Count; i++)
            chkLstBx.SetItemChecked(i, false);

into

for (int i = 1; i < chkLstBx.Items.Count; i++)
{
    chkLstBx.SetItemChecked(i, false);
}

because if you add a second line intending to be within the loop, you may forgot adding the braces - where the program may still be compile and make it hard to find issue. 因为如果您要添加第二行打算放在循环中,则可能会忘记添加花括号-程序可能仍在此处编译,因此很难发现问题。

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

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