日期:2014-05-19  浏览次数:20659 次

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( "购物篮为空! ");