[英]How do I find out if individual items in CheckedListBox are checked? C#
我試過在checkedListBox1.Items中查找,但這沒有幫助。 那么,如何檢查CheckedListBox中的項目是否被標記? 它在Windows窗體應用程序中。
您可以使用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());
});
嘗試這個:
foreach (ListItem item in checkedListBox1.Items)
{
if (item.Selected)
{
// If the item is selected
}
else
{
// Item is not selected, do something else.
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.