gpt4 book ai didi

c# - 将两个列表框中的项目合并到第三个列表框中

转载 作者:行者123 更新时间:2023-11-30 19:56:52 25 4
gpt4 key购买 nike

我正在尝试合并两个列表框的内容,如果列表框 1 中有 A、B、C,而列表框 2 中有 1、2、3,则列表框 3 中的输出将是:A1、A2、 A3、B1、B2、B3、C1、C2、C3。我下面的代码几乎就是这样做的,但是它覆盖了 A 和 B 迭代并且只显示了 C 迭代。我在这里缺少什么?

字符串A;字符串 B;

private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < listBox1.Items.Count; i++)
{
A = listBox1.Items[i].ToString();
}
for (int j = 0; j < listBox2.Items.Count; j++)
{
B = listBox2.Items[j].ToString();
listBox3.Items.Add(A + ": " + B);
}
}

最佳答案

是的,您忽略了一个事实,当第二个循环开始时,第一个循环结束。这就是为什么 A 始终是第一个列表框中的最后一个值。嵌套循环:

private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < listBox1.Items.Count; i++)
{
A = listBox1.Items[i].ToString();

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

listBox3.Items.Add(A + ": " + B);
}
}
}

关于c# - 将两个列表框中的项目合并到第三个列表框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32419864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com