我正在尝试将列表框1中的选定项目移动到列表框2,反之亦然 . 我有两个按钮, >>
和 <<
. 当我在listbox1中选择项目然后单击 >>
时,项目应从listbox1移动到listbox2 .
private void MoveListBoxItems(ListBox source, ListBox destination)
{
ListBox.SelectedObjectCollection sourceItems = source.SelectedItems;
foreach (var item in sourceItems)
{
destination.Items.Add(item);
}
while (source.SelectedItems.Count > 0)
{
source.Items.Remove(source.SelectedItems[0]);
}
}
private void button2_Click_1(object sender, EventArgs e)
{
MoveListBoxItems(listbox , lstActivity);
}
7 回答
你的代码工作正常 . 我测试了它 . 你的问题是“我试图将列表框1中的 selected 项目移到列表框2中 . ”
我认为你的button2有problem.delete button2和下面的代码
然后创建其他按钮并创建单击事件 .
完整来源:
这段代码很有用 . 如果要选择多个项目更改属性SelectionMode = MultiSimple;
每个已删除的行都会有冲突,因此请使用以下代码:
>>
<<
如果以上一个不起作用,那么试试这个:
有关更多类型的答案,您可以使用此link
Here Two ListBoxes. When i select the name from Left listbox and click ">>" Button the data move to Right ListBox
Code .. currentItemText = LeftListBox.SelectedValue.ToString(); currentItemIndex = LeftListBox.SelectedIndex;
我用这个代码解决了:
获取使用2个列表框创建的>>和<<按钮,如下面的listbox2和3 ...首先通过get-content或get-adcomputer等获取列表框2中的项目 .