【问题标题】:Add text from 2 listBox to one listBox将文本从 2 个列表框添加到一个列表框
【发布时间】:2013-08-27 10:59:40
【问题描述】:

如何将 2 个 listBox 中的项目添加到一个 listBox?

例如:listBox1 包含 Hello listBox2 包含 World!因此,如果在 listbox3 中单击 button1 将显示 Hello World!并排,但不是像新行一样

你好

世界!

private void button2_Click(object sender, EventArgs e)
{
  listBox3.Items.Add(listBox1.Items + listBox2.Items);
}

【问题讨论】:

  • 你的listBox1listBox2有多少Items
  • 取决于不包含特定数字的列表。
  • 那么,您想正确添加相同的索引项吗?比如listBox1.Items[0] + listBox2.Items[0]listBox1.Items[1] + listBox2.Items[1]等等……?
  • 我想让它像这样 ex:listbox1 包含 3 个单词,listbox2 有 1 个单词,因此输出将是 listbox1 上的每个单词将与 listbox2 结合。
  • 在你的情况下,你想要listBox3 中的全部 4 个词还是只需要 2 个词(listbox1 的第一个词 + listbox2 的第一个词)?

标签: c# .net


【解决方案1】:
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()));
}

如果您需要两个列表框中的所有单词到一个列表框中

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

更新:

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

【讨论】:

  • 在字符串内部,您有 2 个占位符,但您只提供了一个连接字符串。这将打印出(listBox1 value + listBox2 value) {1}
  • @terrala7 在Items[] 之后和Trim() 之前附加ToString()
  • 这行得通,但是如何使用第一个 listBox 中的多个单词来做到这一点:)
  • 它可以工作,但如果我的 listBox1 包含 Hello Hi Sup 而我的 listBox2 包含 World!我怎样才能让 listBox3 中的输出变成 Hello World!你好世界! Sup World!而不是 Hello Hi Sup World!
  • 谢谢,但不工作它只在 listBox3 中显示 System.Linq.Enumerable+d__7a`3[System.String.System.String.System.String]
【解决方案2】:

既然你说了;

listBox1 中的所有单词都将是 +,而 listBox2 在 listBox3 中

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

但是例如:我的 listBox1 包含 Hello Hi Sup,而我的 listBox2 包含 World! 点击 listBox3 后会变成 Hello Hi Sup World!代替 你好世界!你好世界!支持世界!

如果你想在你的listBox3 中作为一个项目,你可以使用上面的解决方案。如果你想在你的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());
  }

}

【讨论】:

  • 不起作用,这只会将列表从 listBox1 添加到 listBox3
  • @terrala7 否。这会将 listBox1 和 listBox2 的所有成员作为一个项目添加到 listBox3。
  • 操作它可以工作,但例如:我的 listBox1 包含 Hello Hi Sup,我的 listBox2 包含 World!点击 listBox3 后会变成 Hello Hi Sup World!而不是你好世界!你好世界!支持世界!
  • 谢谢,但我认为您在 listBox 编号处拼写错误,它应该是 listBox3.Items.Add(listBox2.Items[j].ToString());而不是 listBox3.Items.Add(listBox1.Items[j].ToString());但它仍然没有做我想做的事情:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-09
相关资源
最近更新 更多