我使用了一个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)索引超出范围求各位帮我看看呀
谢谢
我的想法是 : 使用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)索引超出范围求各位帮我看看呀
谢谢
解决方案 »
- VS 中怎么能让大括号也折叠??????????
- 索引(从零开始)必须大于或等于零,且小于参数列表的大小。
- 框架问题页CS里面的代码不被执行怎么办?
- 如何写关于DataGrid的样式表??
- 有用过DataSet缓存数据的朋友进来帮忙解决解决
- 为什么我用dreamweaver MX下看不到aspx文件里的控件显示如(button, label)等,
- 菜鸟求教sqlconnection问题
- DataGrid选择、编辑、修改、更新,可不可以实现像在VB里面,可以直接在DataGrid中进行编辑????
- 以前都是搞SQL数据库,请问在Web.config中Access的连接代码怎么写?
- 困扰我的问题,ASP.NET 2003 运行不了
- 分页问题
- textbox绑定的时候可不可以在绑定值为空时显示默认值如0等
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);
}
估计原因:
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集合中移除所有的项。
有什么方法使得一次性删除所有的Item 呀