将项目从一个listBox移动到另一个listBox

3

我需要将项目从一个ListBox移动到另一个ListBox。添加到第二个ListBox中可以工作,但我想移动多个所选项目。

private void btnMove_Click(object sender, EventArgs e)
{
    if (listBox2.SelectedItem != null)
    {
       listBox3.Items.Add(listBox2.SelectedItem);
       listBox2.Items.Remove(listBox2.SelectedItem);
    }
}

我的问题是这个程序只能逐个移动物品,但我需要能够在一次点击中移动多个选定的物品。
1个回答

4
  private void Button1_Click(object sender, EventArgs e)

    {
        if (listBox1.SelectedItem != null)
        {
            while (listBox1.SelectedItems.Count > 0)
            {
                listBox2.Items.Add(listBox1.SelectedItem);
                listBox1.Items.Remove(listBox1.SelectedItem);
            }
        }
        else
        {
            MessageBox.Show("No item selected");
        }
    }

1
@ Arthi 我找到了这个链接:http://stackoverflow.com/questions/9951977/how-to-add-selected-item-from-one-listbox-to-another-listbox/33280951#33280951 - user5671888

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接