【发布时间】:2014-03-17 19:32:28
【问题描述】:
所以基本上我使用 FOR 循环从列表框中添加和删除选项。 It functions correctly when selected 1 option (from either remove or select) and it functions correctly when I select two separate options (For example, item[0] and item[4]).
但是,当我尝试选择所有选项或选择并排的两个项目([2]、[3].. 等)时,我遇到了麻烦
这是我的 select 函数循环:
protected void btnSelect_Click(object sender, EventArgs e)
{
for (int intCounter = 0; intCounter < lbSnacks.Items.Count; intCounter++)
{
if (lbSnacks.Items[intCounter].Selected) // if the snack is selected
{ // add the listitem to the lbSelected listbox
lbSelected.Items.Add(lbSnacks.Items[intCounter]);
}
}
for (int intCounter = 0; intCounter < lbSnacks.Items.Count; intCounter++)
{
if (lbSnacks.Items[intCounter].Selected) // if the snack is selected
{ // add the listitem to the lbSelected listbox
lbSnacks.Items.Remove(lbSnacks.Items[intCounter]);
}
}
}
该错误基本上是将项目放入“已选择”列表框中,但在原始“小吃”框中留下了两个选项之一。
有什么想法吗?
【问题讨论】:
标签: c# asp.net for-loop listbox visual-web-developer-2010