如何解决“指数超出范围。必须是非负数且小于集合的大小。在ASP .NET中使用C# [英] How to solve the " index was out of range. Must be non-negative and less than the size of the collection." in ASP .NET using C#

查看:72
本文介绍了如何解决“指数超出范围。必须是非负数且小于集合的大小。在ASP .NET中使用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下

i希望我从列表框中选择一些项目,所选项目会自动添加到另一个列表框并从第一个列表框中删除。
使用c#在asp.net上




我尝试过:



protected void ListBox3_SelectedIndexChanged(object sender,EventArgs e)

{



}

protected void ListBox4_SelectedIndexChanged(object sender,EventArgs e)

{



}

protected void Button6_Click(object sender, EventArgs e)

{

int i;

for(i = 0; i< = ListBox3.Items.Count; i ++)<如果(ListBox3.Items [i] .Selected == true)

{

ListBox4.Items .Add(ListBox3.SelectedItem);

ListBox3.Items.Remove(ListBox3.SelectedItem);

}

}



















}

}

解决方案

提示位于错误消息的小于集合大小部分,因为您正在迭代并包括该大小:

  for (i =  0 ; i <  = ListBox3.Items.Count; i ++)

必须是

  (i =  0 ; i <  ListBox3.Items.Count; i ++)







如理查德所述,您的循环可能会跳过项目,您应该从最后一项开始迭代。

[/编辑]


my code is as follows
i want that i select some items from the list box and the selected items are automatically add on to another list box and remove from the first list box .
in asp.net using c#.

What I have tried:

protected void ListBox3_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void ListBox4_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void Button6_Click(object sender, EventArgs e)
{
int i;
for (i = 0; i <= ListBox3.Items.Count; i++)
{
if (ListBox3.Items[i].Selected == true)
{
ListBox4.Items.Add(ListBox3.SelectedItem);
ListBox3.Items.Remove(ListBox3.SelectedItem);
}
}









}
}

解决方案

The hint is in the "less than size of the collection" part of the error message because you are iterating up to and including that size:

for (i = 0; i <= ListBox3.Items.Count; i++)

It must be

for (i = 0; i < ListBox3.Items.Count; i++) 



[EDIT]
As noted by Richard, your loop might skip items and you should iterating down starting at the last item.
[/EDIT]


这篇关于如何解决“指数超出范围。必须是非负数且小于集合的大小。在ASP .NET中使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆