【问题标题】:How to add data from one listbox to another listbox?如何将数据从一个列表框添加到另一个列表框?
【发布时间】:2019-10-08 13:26:02
【问题描述】:

listbox1 中显示以下数据。我想将项目从 listbox2 传递到 listbox1。结果应该如下图所示(lb1)。如何实现? 非常感谢您的帮助!

var itm = listBox2.Items[0].ToString();
          listBox1.Items.Add(itm);

enter image description here

【问题讨论】:

  • 你想要一份参考资料(项目的纠缠)还是列表的副本?
  • listbox1 中的每一项都用逗号分隔。我想将 listbox2 中的项目附加到同一行中的前一个项目(不是彼此之间)。
  • 你的意思是这样吗? string firstLine = listBox1.Items[0] as string; if (string.IsNullOrEmpty(firstLine)) listBox1.Items[0] = itm; else listBox1.Items[0] += "," + itm;?
  • 正确!那行得通!非常感谢,“mm8” :)

标签: c# listbox


【解决方案1】:

使用AddRange 方法。试试看:

listBox1.Items.AddRange(listBox2.Items);

【讨论】:

  • 那行不通。 listbox1 中的每个项目都用逗号分隔。我想将 listbox2 中的项目附加到同一行中的前一个项目(不是彼此之间) 感谢您的建议“apomene”
【解决方案2】:

你可以使用AddRange

listBox1.Items.AddRange(listBox2.Items);

或者,如果您想要另一个列表框中的内容的精确副本,但不包含您的旧项目。

listBox1.Items = listBox2.Items;

【讨论】:

  • 那行不通。 listbox1 中的每个项目都用逗号分隔。我想将 listbox2 中的项目附加到同一行中的前一个项目(不是彼此之间)谢谢你的建议,马丁
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多