Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在winforms中使用c对checkedlistbox中的项进行索引_C#_Winforms - Fatal编程技术网

C# 在winforms中使用c对checkedlistbox中的项进行索引

C# 在winforms中使用c对checkedlistbox中的项进行索引,c#,winforms,C#,Winforms,我在一个复选框中有10项。我相信第一个元素的索引是0,第二个元素的索引是1,依此类推,直到索引9。我的要求是在检查元素时找到列表中元素的真实索引。目前我正在使用以下代码,但它不会产生期望的结果。例如,当我检查第一个元素“否”时,会显示消息。当我检查第二个元素时,消息说已检查索引0。当我检查第十个元素时,消息说已检查索引1…我的代码有什么问题?请给出建议 private void clbAnnually_ItemCheck(object sender, ItemCheckEventArgs

我在一个复选框中有10项。我相信第一个元素的索引是0,第二个元素的索引是1,依此类推,直到索引9。我的要求是在检查元素时找到列表中元素的真实索引。目前我正在使用以下代码,但它不会产生期望的结果。例如,当我检查第一个元素“否”时,会显示消息。当我检查第二个元素时,消息说已检查索引0。当我检查第十个元素时,消息说已检查索引1…我的代码有什么问题?请给出建议

   private void clbAnnually_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        foreach (int indexChecked in clbAnnually.CheckedIndices)
        {
            MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked");

        }
    }

解决这个问题的方法有点棘手,这是因为
Item
将在
ItemCheck
事件之后进行检查。因此,您实际上需要检查在
ItemCheck
事件期间您当前的
SelectedIndex
是否不在
checkedDies
中,以表明它当前是被选中的:

private void clbAnnually_ItemCheck(object sender, ItemCheckEventArgs e) {           
    if (!checkedListBox1.CheckedIndices.Contains(clbAnnually.SelectedIndex))
        MessageBox.Show("Index#: " + checkedListBox1.SelectedIndex.ToString() + ", is checked");
}

不是真的。我只想知道当前选中元素的真实索引。完美答案。谢谢@Ian。你救了我一天。@gomeshmunda很高兴能帮忙。:)