首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

ListBox problem,该怎么处理

2012-01-16 
ListBox problem我写的一段对ListBox中记录全部删除的语句,可是出现如下问题:如:ListBox中内容为123删除后

ListBox problem
我写的一段对ListBox中记录全部删除的语句,可是出现如下问题:如:
ListBox中内容为
1
2
3
删除后还剩下2。不知道什么原因,高手指导一下
int   i=0;
if   (Lstgoods.Items.Count> 0)
while(i <Lstgoods.Items.Count)
{
Lstgoods.Items.RemoveAt(i);
i=i+1;
}
else
MessageBox.Show( "购物篮为空! ");
注:出于学习考虑没使用Lstgoods.Items.Clear   ();语句
首先谢谢大家

[解决办法]
当Remove一个item以后,你的Lstgoods.Items.Count就少1了,所以你的while循环不能使用Lstgoods.Items.Count,你试试先将Lstgoods.Items.Count赋值给一个变量,然后用while(i <这个变量)的语法试试看
[解决办法]
int i=0;
while(Lstgoods.Items.Count> 0)
{
Lstgoods.Items.RemoveAt(i);
i=i+1;
}
else
MessageBox.Show( "购物篮为空! ");

热点排行