A.sp.net Clear ListBox list items (delete all items) _ Practical Tips

Source: Internet
Author: User
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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.