我如何知道 CheckedListBox 中的个别项目是否被选中? C#
How do I find out if individual items in CheckedListBox are checked? C#
我尝试查看 checkedListBox1.Items,但没有帮助。那么如何检查 CheckedListBox 中的项目是否被标记?它在 windows 表单应用程序中。
试试这个:
foreach (ListItem item in checkedListBox1.Items)
{
if (item.Selected)
{
// If the item is selected
}
else
{
// Item is not selected, do something else.
}
}
您可以使用 CheckedItems
属性.
获取已勾选的项目列表
示例 1:
foreach (var item in this.checkedListBox1.CheckedItems)
{
MessageBox.Show(item.ToString());
}
示例 2:
this.checkedListBox1.CheckedItems.Cast<object>()
.ToList()
.ForEach(item =>
{
//do stuff here
//for example
MessageBox.Show(item.ToString());
});
如果您确定项目是 string
,例如,您可以在上面的代码中使用 Cast<object>
。
您可以使用 CheckedIndices
属性.
获取检查的索引列表
示例:
this.checkedListBox1.CheckedIndices.Cast<int>()
.ToList()
.ForEach(index =>
{
//do stuff here
//for example
MessageBox.Show(this.checkedListBox1.Items[index].ToString());
});
我尝试查看 checkedListBox1.Items,但没有帮助。那么如何检查 CheckedListBox 中的项目是否被标记?它在 windows 表单应用程序中。
试试这个:
foreach (ListItem item in checkedListBox1.Items)
{
if (item.Selected)
{
// If the item is selected
}
else
{
// Item is not selected, do something else.
}
}
您可以使用 CheckedItems
属性.
示例 1:
foreach (var item in this.checkedListBox1.CheckedItems)
{
MessageBox.Show(item.ToString());
}
示例 2:
this.checkedListBox1.CheckedItems.Cast<object>()
.ToList()
.ForEach(item =>
{
//do stuff here
//for example
MessageBox.Show(item.ToString());
});
如果您确定项目是 string
,例如,您可以在上面的代码中使用 Cast<object>
。
您可以使用 CheckedIndices
属性.
示例:
this.checkedListBox1.CheckedIndices.Cast<int>()
.ToList()
.ForEach(index =>
{
//do stuff here
//for example
MessageBox.Show(this.checkedListBox1.Items[index].ToString());
});