在我的应用程序中,用户可以在checkedlistbox
中添加一些项目,然后用户选择某些元素并单击按钮"删除"。如何循环遍历checkedListBox
并删除所选项目?
在我的应用程序中,用户可以在checkedlistbox
中添加一些项目,然后用户选择某些元素并单击按钮"删除"。如何循环遍历checkedListBox
并删除所选项目?
while (checkedListBox1.CheckedItems.Count > 0) {
checkedListBox1.Items.Remove(checkedListBox1.CheckedItems[0]);
}
或者
int lastIndex =checkedListBox1.Items.Count-1;
for(int i=lastIndex ; i>=0 ; i--)
{
if (checkedListBox1.GetItemCheckState(i) == CheckState.Checked)
{
checkedListBox1.Items.RemoveAt(i);
}
}
试一下。这是有效的代码
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
CheckBoxList1.Items.RemoveAt(i);
i--;
}
}
foreach
,它不起作用。你可以使用for循环,但是从后往前删除项目。 - Damith