Move selected items from one listbox to another in

2019-01-14 18:21发布

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);
}

7条回答
爷、活的狠高调
2楼-- · 2019-01-14 19:04
private void buttonMoveToListBox1_Click(object sender, EventArgs e)
{
    if(listBox1.SelectedIndex != -1)
    {
        listBox2.Items.Add(listBox1.SelectedValue);
        listBox1.Items.Remove(listBox1.SelectedValue);
    }
}

private void buttonMoveToListBox2_Click(object sender, EventArgs e)
{
    if(listBox2.SelectedIndex != -1)
    {
        listBox1.Items.Add(listBox2.SelectedValue);
        listBox2.Items.Remove(listBox2.SelectedValue);
    }
}
查看更多
登录 后发表回答