【发布时间】:2021-10-10 02:07:12
【问题描述】:
这只是我想测试的一个简单的东西
我有两种形式(Form1 和 Form2)
在 Form1 中有:
文本框和按钮
当我点击 Form1 中的按钮时,Form2 中只有 TextBox 用于 displayData
问题是当我单击按钮时将数据发送到 Form2,但每次单击它都会创建一个新表单。有什么方法可以在不创建新的情况下更新 Form2 中的数据。
代码如下:
In Form1:
private void sendBt1_Click(object sender, EventArgs e) //sendBt1 is button in Form1
{
Form2 f2 = new Form2(typeBox1.Text);//typeBox1 is TextBox in Form1 use to type
f2.Show();
}
在 Form2 中:
public Form2(string data)
{
InitializeComponent();`enter code here`
this.ds2.Text = data; //ds2 is textBox in Form2 that i wanna display
}
有人可以帮忙吗?我有点卡住了 :)
【问题讨论】:
-
只需使用单例(例如 Form2 中的静态变量
public readonly Instance = new Form2();)或单个实例(private Form2Instance = new Form2();作为 Form1 类的实例成员 - 与单例不同,您如果您愿意,将能够在任何地方创建其他实例,因此如果只需要一个实例,请使用单例,否则在 Form1 中使用静态变量)而不是每次单击按钮时创建表单的对象实例...
标签: c# forms winforms user-interface