我遇到了一个奇怪的问题,我可以将项目从一个列表框移动到另一个列表框,但不能将任何项目移回原始列表框。这是我的代码:
private void MoveListBoxItems(ListBox from, ListBox to)
{
for(int i = 0; i < first_listbox.Items.Count; i++)
{
if (first_listbox.Items[i].Selected)
{
to.Items.Add(from.SelectedItem);
from.Items.Remove(from.SelectedItem);
}
}
from.SelectedIndex = -1;
to.SelectedIndex = -1;
}
protected void Button2_Click(object sender, EventArgs e)
{
MoveListBoxItems(first_listbox, second_listbox);
}
protected void Button1_Click(object sender, EventArgs e)
{
MoveListBoxItems(second_listbox, first_listbox);
}
button2 事件可以正常工作,但是 button1 事件不能。列表框没有数据绑定,我已经手动向它们添加了项目。
也许我在这里遗漏了一些非常明显的东西?
提前感谢您的帮助。