我想遍历一个选中的列表框,看看返回了哪些值。没问题,我知道我可以做到:
if(myCheckedListBox.CheckedItems.Count != 0)
{
string s = "";
for(int i = 0; i <= myCheckedListBox.CheckedItems.Count - 1 ; i++)
{
s = s + "Checked Item " + (i+1).ToString() + " = " + myCheckedListBox.CheckedItems[i].ToString() + "\n";
}
MessageBox.Show(s);
}
问题是当我使用代码生成选中的列表框后想要访问它时。我正在遍历表中的每个控件(在表单上),当控件是选中的列表框时,我需要它来使用我上面编写的代码(或类似代码)。这就是我循环控件的方式:
foreach (Control c in table.Controls)
{
if (c is TextBox)
{
// Do things, that works
}
else if (c is CheckedListBox)
{
// Run the code I've written above
}
问题是,当我尝试访问这样的控件时:,if (c.CheckedItems.Count != 0)
它甚至找不到CheckedItems
. Control c
是否有另一种方法可以访问我选择的控件的该属性并且我看错了?先感谢您。
此致,