在第一个WinForm界面中定义一个公共变量,用于存储第二个WinForm界面中Text文本框的值的具体代码
时间: 2024-03-01 12:54:12 浏览: 117
在第一个WinForm界面中定义一个公共变量,用于存储第二个WinForm界面中Text文本框的值的代码如下:
```
public partial class Form1 : Form
{
public string SecondFormTextBoxValue { get; set; }
// ...
}
```
在这个例子中,我们在 `Form1` 类中定义了一个名为 `SecondFormTextBoxValue` 的公共字符串属性。
接下来,在第二个WinForm界面中,需要将Text文本框的值存储到 `SecondFormTextBoxValue` 变量中:
```
public partial class Form2 : Form
{
private void button1_Click(object sender, EventArgs e)
{
// 获取Text文本框的值
string textBoxValue = textBox1.Text;
// 将Text文本框的值存储到Form1中的公共变量中
Form1 form1 = new Form1();
form1.SecondFormTextBoxValue = textBoxValue;
}
// ...
}
```
在这个例子中,我们在 `Form2` 类中定义了一个名为 `button1` 的按钮,当按钮被点击时,我们获取 `textBox1` 文本框的值并将其存储到 `Form1` 中的 `SecondFormTextBoxValue` 公共变量中。
注意,我们在 `Form2` 类中实例化了一个新的 `Form1` 对象,这意味着我们可以访问 `Form1` 类中的公共变量和属性。这个例子中并没有显示 `Form1` 窗体,这只是为了演示如何访问公共变量。在实际应用中,需要先显示 `Form1` 窗体,然后再打开 `Form2` 窗体,并在 `Form2` 窗体中访问 `Form1` 窗体中的公共变量。
阅读全文
相关推荐


















