【发布时间】:2010-06-24 08:49:21
【问题描述】:
我想遍历一个选中的列表框并查看返回了哪些值。没问题,我知道我可以做到:
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),它甚至找不到Control c 的CheckedItems 属性。是否有另一种方法可以访问我选择的控件的该属性并且我看错了?提前谢谢你。
此致,
【问题讨论】:
标签: c# forms loops checkedlistbox