How to clear the ListBox list item (delete all items), and try to use this feature when you develop the program today. It didn't go very well at first. When you recycle all item to remove, you need to perform two times before you can complete the cleanup. Debug carries out a stepping trace, finding that Count decreases with each removal of the Listbox.Items.Count, while capacity does not change accordingly.
Search for relevant information on the Web, a considerable number of users have the same requirements, one remove the listbox list all items. Methods are used:
Copy Code code as follows:
for (int i = 0; i < Listbox1.Items.Count; i++)
{
Listbox1.Items.RemoveAt (i);
}
Or:
Copy Code code as follows:
foreach (ListItem li in Listbox1.items)
{
ListBox1.Items.Remove (LI);
}
The latter may appear to be abnormal: Collection was modified; Enumeration operation may not execute.
Anyway, here's the insus.net workaround, write an iterator:
Copy Code code as follows:
private void Iterationremoveitem (ListBox listbox)
{
for (int i = 0; I < ListBox. Items.Count; i++)
{
This. ListBoxCondition.Items.RemoveAt (i);
}
for (int j = 0; J < ListBox.) Items.Count; J + +)
{
Iterationremoveitem (ListBox);
}
}
Write in the Purge Ammonium button event:
Copy Code code as follows:
protected void Buttonclear_click (object sender, EventArgs e)
{
Iterationremoveitem (this. LISTBOX1);
}
You can see the action effect from the following: