选中复选框选定索引

hex*_*x c 1 c# if-statement selectedindex messagebox

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

它在索引2本身被选中时起作用但在我的核对表框中选中包括索引2在内的其他项目时它不起作用.下面是我的工作,现在我只需要它工作时选择2和其他人.

if (checkedListBox1.SelectedIndex == 2)
{
   MessageBox.Show("Note to send email", "Note", MessageBoxButtons.OK);
}
Run Code Online (Sandbox Code Playgroud)

Szy*_*mon 5

用.替换该代码

if (checkedListBox1.SelectedIndices.Contains(2))
{
   MessageBox.Show("Note to send email", "Note", MessageBoxButtons.OK);
}
Run Code Online (Sandbox Code Playgroud)

这将检查所有选定项目中是否有2.

SelectedIndicesMSDN上查看有关属性的更多信息.