我遇到一個奇怪的問題,我可以將項目從一個列表框移動到另一個列表框,但不能將任何項目移回原始列表框。這裏是我的代碼:將項目從一個列表框移動到另一個列表框(c#webforms)
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事件沒有。列表框不是數據綁定的,我已經手動向它們添加了項目。
也許有什麼非常明顯的,我在這裏失蹤?
感謝您的幫助提前。
謝謝MusiGenesis。愚蠢的錯誤由我。正如他們所說的那樣,一組新的眼睛...... – Mike91
NominSim還發現了另一個錯誤,如果您選擇了多個項目,這將不起作用。 – MusiGenesis