简体   繁体   English

将文本从2个列表框添加到一个列表框

[英]Add text from 2 listBox to one listBox

How can I add items from 2 listBox to one listBox? 如何将项目从2个列表框添加到一个列表框?

ex: listBox1 contain Hello listBox2 contain World! 例如:listBox1包含Hello listBox2包含World! So if button1 is clicked in listbox3 will display Hello World! 因此,如果在列表框3中单击button1,将显示Hello World! side bye side but not in a new line like 并排,但不要换行

Hello 你好

World! 世界!

private void button2_Click(object sender, EventArgs e)
{
  listBox3.Items.Add(listBox1.Items + listBox2.Items);
}
private void button2_Click(object sender, EventArgs e)
{
  listBox3.Items.Add(string.Format("{0} {1}", listBox1.Items[0].ToString().Trim() , listBox2.Items[0].ToString().Trim()));
}

if you need all words in two list box to a one list box 如果您需要两个列表框中的所有单词到一个列表框中

private void button2_Click(object sender, EventArgs e)
{
  listBox3.Items.Add( string.Format("{0} {1}", string.Join(" ", listBox1.Items.Cast<string>()) , string.Join(" ", listBox2.Items.Cast<string>())));
}

Update : 更新:

private void button2_Click(object sender, EventArgs e)
{
  listBox3.Items.AddRange(listBox1.Items.Cast<string>().Zip(listBox2.Items.Cast<string>(), (first, second) => first + " " + second).ToArray());
}

Since you said; 因为你说过

all words in listBox1 will be + with listBox2 in listBox3 listBox1中的所有单词将与listBox3中的listBox2一起为+

private void button2_Click(object sender, EventArgs e)
{
  string s = "";
  for(int i = 0; i < listBox1.Items.Count; i++)
  {
      s += listBox1.Items[i].ToString() + " ";
  }

  for(int j = 0; j < listBox2.Items.Count; j++)
  {
      s += listBox2.Items[j].ToString() + " ";
  }

  listBox3.Items.Add(s.Trim());
}

but ex:my listBox1 contain Hello Hi Sup and my listBox2 contain World! 但是ex:my listBox1包含Hello Hi Sup,而listBox2包含World! after click in listBox3 it will become Hello Hi Sup World! 在listBox3中单击后,它将变为Hello Hi Sup World! instead of Hello World! 而不是Hello World! Hi World! 你好世界! Sup World! Sup World!

If you want as a one item in your listBox3 , you can use upper solution. 如果要作为listBox3中的listBox3 ,则可以使用上层解决方案。 If you want as total 4 item in your listBox3 , you can use it like; 如果要在listBox3总共设置4个项目,则可以像这样使用它:

private void button2_Click(object sender, EventArgs e)
{

  for(int i = 0; i < listBox1.Items.Count; i++)
  {
      listBox3.Items.Add(listBox1.Items[i].ToString());
  }

  for(int j = 0; j < listBox2.Items.Count; j++)
  {
      listBox3.Items.Add(listBox2.Items[j].ToString());
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM