C# 选中复选框选定索引

C# 选中复选框选定索引,c#,if-statement,messagebox,selectedindex,C#,If Statement,Messagebox,Selectedindex,我的代码中有一个简单的IF语句,如果选中了checkedlistbox1的索引2中的项,我想提示用户 if (checkedListBox1.SelectedIndex == 2) { MessageBox.Show("Note to send email", "Note", MessageBoxButtons.OK); } 当选择了索引2本身时,它会起作用,但当在“我的检查表”框中选中包括索引2在内的其他项目时,它不会起作用。下面是我有什么工作,现在我只需要它的工作时,2和其他人被选中

我的代码中有一个简单的IF语句,如果选中了checkedlistbox1的索引2中的项,我想提示用户

if (checkedListBox1.SelectedIndex == 2)
{
   MessageBox.Show("Note to send email", "Note", MessageBoxButtons.OK);
}
当选择了索引2本身时,它会起作用,但当在“我的检查表”框中选中包括索引2在内的其他项目时,它不会起作用。下面是我有什么工作,现在我只需要它的工作时,2和其他人被选中

if (checkedListBox1.SelectedIndex == 2)
{
   MessageBox.Show("Note to send email", "Note", MessageBoxButtons.OK);
}
将该代码替换为

if (checkedListBox1.SelectedIndices.Contains(2))
{
   MessageBox.Show("Note to send email", "Note", MessageBoxButtons.OK);
}
这将检查2是否在所有选定项目中

请参阅上SelectedDices属性的详细信息。

将该代码替换为

if (checkedListBox1.SelectedIndices.Contains(2))
{
   MessageBox.Show("Note to send email", "Note", MessageBoxButtons.OK);
}
这将检查2是否在所有选定项目中

请参阅上的SelectedDices属性的详细信息。

使用SelectedDices而不是SelectedIndex,它将返回选定索引的集合。只需确保2在其中。

使用SelectedDices而不是SelectedIndex,它将返回所选索引的集合。只要确保里面有2个就可以了。

你可以用它来代替

此CheckedListBox中选中索引的集合

您可以使用

此CheckedListBox中选中索引的集合


工作得很好!!非常感谢。工作得很好!!非常感谢。