【问题标题】:How to retrieve if an item in a CheckedListBox is checked or not? Winforms C#如何检索 CheckedListBox 中的项目是否被选中? C#
【发布时间】:2016-07-06 09:04:15
【问题描述】:

我有一个foreach() 语句贯穿CheckedListBox 中的所有项目。

我如何知道某个项目是否已检查?

如果有用的话,代码如下:

foreach (object user in checkedListBoxUsersWhoSee.Items)
{
    // Privileged = if the user is checked he has privileges;
    alias = user.ToString().Substring(user.ToString().Length - 3);
    SelectUserID = new SqlCommand(Properties.Resources.SelectUserID + alias, TeamPWSecureBD);
    userIDAuth = (int)SelectUserID.ExecuteScalar();

    InsertAuths.Parameters.AddWithValue("@idPass", idPass);
    InsertAuths.Parameters.AddWithValue("@idUser", userIDAuth);
    InsertAuths.Parameters.AddWithValue("@Privileged", idPass);

    //Code not finished
}

【问题讨论】:

  • isChecked() 或类似的东西? (user.isChecked()?)
  • @Furtiro 我希望就这么简单
  • CheckedListBox 有一个属性CheckedItems,它是选中项的集合。
  • CheckState bool 复杂一点

标签: c# winforms checkboxlist


【解决方案1】:
  for (int i = 0; i < checkedListBoxUsersWhoSee.Items.Count; i++)
  {
    CheckState checkState = checkedListBoxUsersWhoSee.GetItemCheckState(i);
    //CheckState.Checked
    //CheckState.Indeterminate
    //CheckState.Unchecked
  }

【讨论】:

    【解决方案2】:

    您可以使用此代码:

    foreach (object user in checkedListBox.Items)
    {
        bool Privileged = checkedListBox.GetItemCheckState(checkedListBox.Items.IndexOf(user)) == CheckState.Checked;
    }
    

    【讨论】:

    • 这实际上是错误的。 CheckedItems 集合包括 Checked 和 Indeterminate 项。
    【解决方案3】:

    试试

    foreach (CheckBox user in   checkedListBox1.CheckedItems)
    {
    
    }
    

    【讨论】:

    • CheckedItems 集合包括 Checked 和 Indeterminate 项。
    【解决方案4】:

    CheckedListBox 有一个属性CheckedItems,它是选中或不确定项的集合。

    var items = checkedListBoxUsersWhoSee.CheckedItems;
    

    更新 我测试了将项目添加到 CheckedListBox 并且它们没有出现在 CheckedItems 属性下,这表明默认情况下它们使用值 Unchecked 进行初始化。

    【讨论】:

    • 错了。 CheckedItems 集合包括 Checked 和 Indeterminate 项。
    • 仔细阅读:Remarks: The collection is a subset of the objects in the Items collection, representing only those items whose System.Windows.Forms.CheckState is Checked or Indeterminate. The indexes in this collection are in ascending order.
    • 这是一个公平的观点,但在测试创建 CheckedListBox 并向其中添加项目后,它们都没有出现在 CheckedItems 属性中。这表明添加的默认值是Unchecked 而不是Indeterminate。所以答案还是正确的。
    • 好吧,如果状态永远不会改变,我们无论如何都不需要测试它们。不,如果它说这是已检查或不确定项的集合..,则答案是正确的
    【解决方案5】:

    @Arvin 给出了正确的答案,但 idkw 他编辑了一个更令人困惑的解决问题的方法。

    下面的代码就像一个魅力,所以请编辑正确答案的人不要乱来。

    foreach (object user in checkedListBoxUsersWhoSee.Items)
    {
          Privileged = checkedListBoxUsersWhoSee.CheckedItems.Contains(user);
          ...
    }
    

    【讨论】:

      【解决方案6】:

      我使用了以下内容:

                      ArrayList selected = new ArrayList();
                      for (int i = 0; i < chkRoles.Items.Count; i++) //chkRoles being the CheckBoxList
                      {
                          if (chkRoles.GetItemChecked(i))
                              selected.Add(chkRoles.Items[i].ToString()); //And I just added what was checked to the Arraylist. String values
                      }
      

      【讨论】:

        【解决方案7】:

        最简单的方法:

        foreach(ListItem item in checkedListBoxUsersWhoSee.Items){    
           if (item.Selected){
               // LOGIC HERE
           }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-04-02
          • 1970-01-01
          • 1970-01-01
          • 2015-10-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-28
          相关资源
          最近更新 更多