日期:2014-05-18  浏览次数:21202 次

遍历CheckedListBox中的选中项
在winform程序中怎么样遍历CheckedListBox中的选中项?
用for循环和foreach分别怎么写?

------解决方案--------------------
http://msdn2.microsoft.com/zh-cn/library/e954th47(VS.80).aspx
------解决方案--------------------
for (int n = 0; n < CheckBoxList1.Items.Count; n++)
{
if (CheckBoxList1.Items[n].Selected)
{
string str = CheckBoxList1.Items[n].Text; //获取此项
}
}
foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Selected)
{
string str1 = item.Text;
}
}
------解决方案--------------------
楼上正解!
------解决方案--------------------
int count = checkedListBox1.Items.Count;
for (int i = 0;i<count;i++)
{
if (checkedListBox1.GetItemChecked(i))
{
MessageBox.Show(checkedListBox1.Items[i].ToString());
}
}
------解决方案--------------------
2楼正确
------解决方案--------------------
for (int n = 0; n < CheckBoxList1.Items.Count; n++) 

if (CheckBoxList1.Items[n].Selected) 

string str += CheckBoxList1.Items[n].Text; //获取此项 


foreach (ListItem item in CheckBoxList1.Items) 

if (item.Selected) 

string str1 += item.Text; 

}
------解决方案--------------------
for (int n = 0; n < CheckBoxList1.Items.Count; n++)
{
if (CheckBoxList1.Items[n].Selected)
{
string str += CheckBoxList1.Items[n].Text; //获取此项
}
}
foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Selected)
{
string str1 += item.Text;
}
}
------解决方案--------------------
ListItem item 换成 ListViewItem item
------解决方案--------------------
我自己编译都是对的哦```LZ