如何清除ListBox的列表项(删除所有项目), 今天开发程序时,有尝试使用此功能。一开始并不是很顺利。循环所有item去做remove时,需要执行两次才可以完成清除。debug进行步进跟踪,发现在Listbox.Items.Count 每移除一个,Count随之减少,而Capacity并没有作相应变化。  
	
	 在网上搜索相关资料,相当多用户有相同要求,一次移除ListBox的列表所有项。方法均是用:  
复制代码 代码如下:
	
	for (int i = 0; i < Listbox1.Items.Count; i++) 
	{ 
	Listbox1.Items.RemoveAt(i); 
	} 
	
	 或者:  
复制代码 代码如下:
	
	foreach (ListItem li in ListBox1.Items) 
	{ 
	ListBox1.Items.Remove(li); 
	} 
	
	 而后者会出现异常: Collection was modified; enumeration operation may not execute.  
	 不管怎样,下面是Insus.NET的解决方法,写一个迭代器:  
复制代码 代码如下:
	
	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); 
	} 
	} 
	
	 在清除铵钮事件中写:  
复制代码 代码如下:
	
	protected void ButtonClear_Click(object sender, EventArgs e) 
	{ 
	IterationRemoveItem(this.ListBox1); 
	} 
	
	 可以从下面看到操作效果:  
查看更多关于asp.net清除ListBox的列表项(删除所有项目)的详细内容...