【发布时间】:2015-04-26 00:25:39
【问题描述】:
我想将数据从Form2 传输到Form1 的listBox1。 Form2 包含几个文本框,
我试图通过将所有这些代码放入Form1 来解决这个问题,但我无法从Form2 获得所需的值。
谁能帮忙,我知道我不是很清楚,但我可以回答有关代码的任何问题。
private void button3_Click(object sender, EventArgs e)
{
List<Form1Other> FileList = new List<Form1Other>();
Form1Other[] f1Other = new Form1Other[10];
Form1 testForm1 = new Form1();
string TName = textBox1.Text;
string TDesc = textBox2.Text;
decimal TPrior = numericUpDown1.Value;
string TDate = dateTimePicker1.Value.ToShortDateString();
string TCompl = "UNFINISHED";
FileList.Add(new Form1Other(TName, TDesc, TPrior, TDate, TCompl));
testForm1.listBox1.Items.Add(FileList[0].tName);
testForm1.listBox1.Items.Add(FileList[0].tDesc);
testForm1.listBox1.Items.Add(FileList[0].tPrior);
testForm1.listBox1.Items.Add(FileList[0].tDate);
testForm1.listBox1.Items.Add(FileList[0].tCompl);
System.Diagnostics.Debug.WriteLine(FileList[0].tDesc);
【问题讨论】:
-
您想要Form1 中的Form2 数据?他们是不同的班级吗?如果是这样,您可以在form2中设置一个公共属性然后在form1中获取它吗?
-
您的代码示例中的哪个位置是
Form2?Form1Other是什么?从Form1Other的全新实例中检索值有什么好处? IE。 its 值是如何被初始化的?如果您发布的代码已经在Form1中,为什么要创建Form1的新实例?为什么要从当前表单(您说是Form1)的控件中检索值,而不是从Form2的实例中检索值?你是对的......你的问题远非明确。你需要改进它很多才能得到一个好的答案。 -
注意:这个重复的问题很久以前就被问过/回答了,从那时起,许多其他类似的问题也被问过和回答了。但就它而言,那是相当不错的。我刚刚添加了一个新答案,以详细说明其他人对已接受答案的评论,以展示另一种策略。在您的情况下,这些答案中的任何一个都应该适用。请注意,两者都假设在某个时候,一个表单对象引用了另一个表单对象;你没有在这里发布足够的代码让任何人建议如何在你的情况下这样做,但这对你来说应该不难弄清楚
标签: c# winforms data-transfer