在选中的 checkedListBox 中确定(取消)选中的复选框

Determining (de)selected checkbox in a checkedListBox as they are selected

我想在 C# 中设置一个 checkedListBox。这个 checkedListBox 中将显示大约 50 个名称。 I'd like it so that when a name is either selected or deselected, then the program will be able to store it in a variable.

我已经有了代码,这样当按下一个按钮时,它会搜索 checkedListBox 中的所有名称和 returns 所有具有选定状态的名称:

for (int i = 0; i < nameBox.CheckedItems.Count; i++)
{
          ArrayList.Add(nameBox.CheckedItems[i]);
}

我知道在 Java 中我可以使用 e.getStateChange() 来确定哪个项目已被选择或取消选择:

public void itemStateChanged(ItemEvent e) 
{
        if(e.getStateChange() == ItemEvent.SELECTED)
    {
              ArrayList = checkbox.getText();
        }    
}

我可以在 C# 中为 checkedListBox 使用与此 Java 代码类似的代码吗?

任何 help/advice 将不胜感激!

基于评论和 link,其中包含您在示例中需要的所有内容:

在您的表单中添加私有变量,例如列表:

 public class Form1 : System.Windows.Forms.Form
   {
      private System.Windows.Forms.CheckedListBox checkedListBox1;
      private List<string> extraVariable;

然后在您的构造函数中或在您初始化检查列表的任何地方,也初始化您的额外变量:

      public Form1()
      {
         InitializeComponent();

         extraVariable = new List<string>();

然后添加您的 ItemChecked 事件,您可以在其中添加或删除您的额外变量:

      private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
      {
         if(e.NewValue==CheckState.Unchecked)
         {
            extraVariable.Remove(e.NewValue);
         }
         else
         {
            extraVariable.Add(e.NewValue);
         }
      }