top*_*eel 3 foreach checkedlistbox winforms
我正在尝试检查已检查列表框中的每个项目,并根据是否选中该项目执行某些操作.我已经能够使用indexCollection获取已检查的项目,但是我无法获取所有项目,我收到此错误'无法将类型为'System.String'的对象强制转换为'System'. Windows.Forms.ListViewItem"
foreach (ListViewItem item in checkedListBox2.Items)
{
if (item.Checked == true)
//do something with item.name
else
//do something else with item.name
}
Run Code Online (Sandbox Code Playgroud)
我不确定为什么它给了我foreach行中的字符串转换错误.我怎么能做到这一点?谢谢.
如果允许复选框具有Indeterminate状态,则应使用GetItemCheckState方法检索复选框的状态
for (int i = 0; i < checkedListBox2.Items.Count; i++)
{
CheckState st = checkedListBox2.GetItemCheckState(checkedListBox2.Items.IndexOf(i));
if(st == CheckState.Checked)
....
else if(st == CheckState.Unchecked)
....
else
... // inderminate
}
Run Code Online (Sandbox Code Playgroud)
否则足以调用返回true/false值的GetItemChecked(对于不确定状态也是如此)
| 归档时间: |
|
| 查看次数: |
11199 次 |
| 最近记录: |