源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

a.sp.net清除ListBox的列表项(删除所有项目)

  • 时间:2020-07-23 16:29 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:a.sp.net清除ListBox的列表项(删除所有项目)
如何清除ListBox的列表项(删除所有项目), 今天开发程序时,有尝试使用此功能。一开始并不是很顺利。循环所有item去做remove时,需要执行两次才可以完成清除。debug进行步进跟踪,发现在Listbox.Items.Count 每移除一个,Count随之减少,而Capacity并没有作相应变化。 在网上搜索相关资料,相当多用户有相同要求,一次移除ListBox的列表所有项。方法均是用:
[u]复制代码[/u] 代码如下:
for (int i = 0; i < Listbox1.Items.Count; i++) { Listbox1.Items.RemoveAt(i); }
或者:
[u]复制代码[/u] 代码如下:
foreach (ListItem li in ListBox1.Items) { ListBox1.Items.Remove(li); }
而后者会出现异常: Collection was modified; enumeration operation may not execute. 不管怎样,下面是Insus.NET的解决方法,写一个迭代器:
[u]复制代码[/u] 代码如下:
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); } }
在清除铵钮事件中写:
[u]复制代码[/u] 代码如下:
protected void ButtonClear_Click(object sender, EventArgs e) { IterationRemoveItem(this.ListBox1); }
可以从下面看到操作效果: [img]http://files.jb51.net/upload/201201/20120109214329739.gif[/img]
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部