【问题标题】:Checked items in a CheckedListBoxCheckedListBox 中的选中项
【发布时间】:2015-06-18 09:42:53
【问题描述】:

C# 中,如果我有CheckListBoxItem 的文本,我如何确定CheckedListBox 中的项目是否被选中?

我需要遍历所有CheckedListBoxItems,并检索文本和选中状态。

这是我目前所拥有的:

CheckedListBox.ObjectCollection items = checkedListBoxFileNames.Items;
foreach (var item in items)
{

}

我不确定如何确定一个项目是否被选中。

提前致谢。

【问题讨论】:

  • 您需要所有已检查的项目吗?您可以使用 CheckedItems 属性

标签: c# winforms boolean checkedlistbox


【解决方案1】:

你不需要这个 foreach 循环。

试试这个:

if(this.m_CheckedListbox.CheckedItems.Contains("Item1")
{
   //make an action, if it's checked.
}

if(this.m_CheckedListbox.CheckedItems.Contains("Item2")
{
   //make an action, if it's checked.
}

// etc...

// this.m_CheckedListbox should be the name of your checked list box.

【讨论】:

    【解决方案2】:

    试试这样:

    foreach(object itemChecked in checkedListBox1.CheckedItems)
    {
         //your code
    }
    

    同时检查CheckedListBox.CheckedItems Property

    此 CheckedListBox 中选中项的集合。

    【讨论】:

      【解决方案3】:

      你可以像这样使用

      IEnumerable<int> allChecked = (from item in chkBoxList.Items.Cast<ListItem>() 
                                 where item.Selected 
                                 select int.Parse(item.Value));
      

      更多详情click here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多