【问题标题】:How to copy items from one listbox in one tab page to another listbox in another tab page?如何将项目从一个标签页中的一个列表框复制到另一个标签页中的另一个列表框?
【发布时间】:2014-08-11 14:06:35
【问题描述】:

问题是我无法将项目从一个列表框复制到另一个列表框。除此之外,列表框位于标签控件的不同标签页中。

我尝试了一些代码,例如:

private void MoveListBoxItems(Flowerlistbox, Total1istBox)

{        
    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 Donebutton_Click(object sender, EventArgs e)       
{

try

{

 //moving all items from listbox1 to listbox2

Total1listBox.Items.AddRange(FlowerlistBox.Items);

  FlowerlistBox.Items.Clear();

 }

 catch (Exception ex)

 {

    MessageBox.Show(ex.Message);

   }

     Total1listBox.SelectedIndex = 0;

 }



foreach(var item in FlowerlistBox.SelectedItems)

{    
Total1listBox.Items.Add(item);

}




private void Donebutton_Click(object sender, EventArgs e)

{

              foreach(var item in FlowerlistBox.SelectedItems)

           {

             Total1listBox.Items.Add(item);

           }

但是它们似乎都没有工作。

【问题讨论】:

  • 您好,您能否提供一些代码,以便我们为您提供帮助。
  • 您的问题解决了吗?

标签: c# tabs listbox listboxitem


【解决方案1】:

这将首先从FlowerlistBox中收集选定的项目,然后将它们添加到Total1istBox,然后将它们从FlowerlistBox中删除,最后清除列表。

请注意,您的代码对 FlowerlistBox 有不同的拼写,可能应该拼写为 TotalListBox。

List<object> theItems = new List<object>();
foreach (object item in FlowerlistBox.SelectedItems) theItems.Add(item);
foreach (object item in theItems) Total1istBox.Items.Add(item);
foreach (object item in theItems) FlowerlistBox.Items.Remove(item);
theItems.Clear();

首先收集项目的原因是,当我们最终想要删除它们时,我们无法修改我们需要迭代的集合。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多