我使用了一个DropDownList和一个ListBox控件
我的想法是 : 使用DropDownList改变ListBox中的值
但是第一次使用后,我想删除ListBox中的值
DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)

增加ListBOx中的值,使用的是ListBox.Items.Add()方法;

private void Page_Load(object sender, System.EventArgs e)

 int count=ListBox1.Items.Count;
 for(int i=0;i<count-1;i++)
  {
   ListBox1.Items.RemoveAt(i);
  }
}但是总是出错说ListBox1.Items.RemoveAt(i)索引超出范围求各位帮我看看呀
谢谢

解决方案 »

  1.   

    是你的程序逻辑不对:
     int count=ListBox1.Items.Count;
     for(int i=0;i<count-1;i++)
      {
       ListBox1.Items.RemoveAt(i);
      }
    在你删除之后ListBox1.Items.Count就少了!
    这样试试吧:
     int count=ListBox1.Items.Count; for(int i=count-1;i>=0;i--)
      {
       ListBox1.Items.RemoveAt(i);
      }
      

  2.   

    这是可能的。
    估计原因:
    1 int count=ListBox1.Items.Count; 是最大长度时的值;
    2 ListBox1.Items.RemoveAt(i);执行后,ListBox1.Items.Count已经变小了,但count值没有修改,肯定会“索引超出范围”;a>
    private void Page_Load(object sender, System.EventArgs e)

     int count=ListBox1.Items.Count-1;
     for(int i=count;i>=0;i--)
      {
       ListBox1.Items.RemoveAt(i);
      }
    }b>
        调用Clear(),从ListBox.ObjectCollection集合中移除所有的项。
      

  3.   

    对了
    有什么方法使得一次性删除所有的Item 呀