移动选定的项目从一个列表框到另一个在C#中的winform [英] Move selected items from one listbox to another in C# winform
问题描述
我想移动选定项目列表中BOX1列出BOX2,反之亦然。我有两个按钮,>>
和<<
。当我选择在ListBox1的项目,然后点击>方式>
的项目应当从ListBox1中移动到listbox2
私人无效MoveListBoxItems(列表框源,列表框目的地)
{
ListBox.SelectedObjectCollection sourceItems = source.SelectedItems;
的foreach(在sourceItems VAR项)
{
destination.Items.Add(项目);
}
而(source.SelectedItems.Count大于0)
{
source.Items.Remove(source.SelectedItems [0]);
}
}私人无效button2_Click_1(对象发件人,EventArgs的发送)
{
MoveListBoxItems(列表框,lstActivity);
}
您code正常工作。我测试了它。
你的问题是我尽量在列表BOX1移动选项列出BOX2。
我觉得你的按钮2有problem.delete按钮2及以下
在code 私人无效button2_Click_1(对象发件人,EventArgs的发送)
{
MoveListBoxItems(列表框,lstActivity);
}
然后创建其他按钮,然后创建click事件。
完整的源代码:
私人无效MoveListBoxItems(列表框源,列表框目的地)
{
ListBox.SelectedObjectCollection sourceItems = source.SelectedItems;
的foreach(在sourceItems VAR项)
{
destination.Items.Add(项目);
}
而(source.SelectedItems.Count大于0)
{
source.Items.Remove(source.SelectedItems [0]);
}
}私人无效first2second_Click(对象发件人,EventArgs的发送)
{
MoveListBoxItems(FirstListbox,LastListbox);
}私人无效second2first_Click(对象发件人,EventArgs的发送)
{
MoveListBoxItems(LastListbox,FirstListbox);
}
这code是工作。如果你想选择一个以上的项目更改属性
的SelectionMode = MultiSimple;
I'm trying to move selected items in list box1 to list box2, and vice versa. I have two buttons, >>
and <<
. When I select items in listbox1 and then click on >>
the items should move from listbox1 to 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);
}
your code works fine. i tested it. your question is "I try to move selected item in list box1 to list box2."
i think your button2 has problem.delete button2 and the code below
private void button2_Click_1(object sender, EventArgs e)
{
MoveListBoxItems(listbox , lstActivity);
}
then create other button and create click event.
full source:
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 first2second_Click(object sender, EventArgs e)
{
MoveListBoxItems(FirstListbox, LastListbox);
}
private void second2first_Click(object sender, EventArgs e)
{
MoveListBoxItems(LastListbox, FirstListbox);
}
this code is work. if you want select more than one item change property SelectionMode = MultiSimple;
这篇关于移动选定的项目从一个列表框到另一个在C#中的winform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!