简体   繁体   English

选中的列表框选中的项目验证

[英]Checked list box Checked items Verification

I have a Checkedlistbox that have the following information: 我有一个Checkedlistbox,其中包含以下信息:

*************
*__All Cells*
*__Cell A   *
*__Cell B   *
*__Cell C   *
*__Cell D   *
*************

I want to check every field that i want, however i want to if I check the "All Cells" Checkbox, all the fields have to checked automatically, I already can do this. 我想检查我想要的每个字段,但是如果我选中“所有单元格”复选框,所有字段都必须自动检查,我已经可以执行此操作。 The part that need help if that I want that when i uncheck the "All Cells" checkbox all the cells are supposed to unchecked. 需要帮助的部分,如果我想取消选中“所有单元格”复选框时,所有单元格都应取消选中。

Here is the code that i use. 这是我使用的代码。 Please help me with this. 请帮我解决一下这个。

private void Cells_CheckedListBox_SelectedIndexChanged(object sender, EventArgs e)
{       
    if (Cells_CheckedListBox.GetItemChecked(Cells_CheckedListBox.Items.IndexOf("All Cells")))
    {
        for (int i = 0; i < Cells_CheckedListBox.Items.Count; i++)
        {
            Cells_CheckedListBox.SetItemCheckState(i, CheckState.Checked);
        }
    } 
}

You seem to be doing this in the wrong event altogether. 您似乎完全是在错误的事件中执行此操作。 You should handle the ItemCheck event. 您应该处理ItemCheck事件。

private void Cells_CheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
{
    int allIndex = Cells_CheckedListBox.Items.IndexOf("All Cells");
    if (e.Index == allIndex)
    {
        for (int i = 0; i < Cells_CheckedListBox.Items.Count; i++)
        {
            if (i != allIndex)
                Cells_CheckedListBox.SetItemCheckState(i, e.NewValue);
        }
    }
}

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

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