我似乎在将一些字符串从一种形式传递到另一种形式时遇到问题。我在 Form1 上有两个 listBoxes,但我想使用 Form2 将信息传递到 Form1 中的 listBoxes 中。在 Form2 中,我有一个 button_Click 函数,它使用参数字符串 a 和字符串 b 调用 Form1 中的函数。
private void button_Click(object sender, EventArgs e){
Form1 frm = new Form1();
frm.AddItemToListBox(txtBox1.Text,txtBox2.Text);
}
下面是在 Form1 中调用函数 AddItemToListBox 的代码。
以上是Form1中的AddItemToListBox函数。
AddItemToListBox(string a, string b){
listBox1.Items.Add(a);
listBox2.Items.Add(b);
}
但是,这不会将字符串添加到列表框中。没有错误消息或任何东西。我也尝试过声明 2 个公共字符串 STR1 和 STR2
AddItemToListBox(string a, string b){
listBox1.Items.Add(a);
listBox2.Items.Add(b);
STR1 = a;
STR2 = b;
}
但是,如果我使用显示 STR1 和 STR2 值的按钮。它们将是“”,String.Empty。显示该值的 MessageBox 中没有显示任何内容。但是,如果我放置 MessageBox.Show(a); 和 MessageBox.Show(b); 在 AddItemToListBox 函数中,字符串将正确显示但仍未添加到列表框中。
这让我沮丧了 2 个小时,我想知道为什么以及如何实际使用该方法将项目添加到列表框中(如果可能)。提前致谢。