如何:从其他窗体中获取值 (Visual C#)

更新:2007 年 11 月

本示例从 Windows 窗体上的一个文本框中检索值,然后在另一个窗体上的文本框中显示该值。

示例

// In Form1.cs.
private Form2 otherForm = new Form2();
private void GetOtherFormTextBox()
{
    textBox1.Text = otherForm.TextBox1.Text;
}
private void button1_Click(object sender, EventArgs e)

    GetOtherFormTextBox();
}

编译代码

此示例需要:

  • 两个名为 Form1 和 Form2 的窗体,每个窗体包含一个名为 textBox1 的 TextBox 控件。Form1 应创建 Form2 的实例,并将其赋给 otherForm;GetOtherFormTextBox 将 Form2 上 textBox1 中的文本复制到 Form1 上的 textBox1。

  • 在设计时应该将字符串赋给 Form2 上 textBox1 的“Text”属性。

请参见

概念

在 Visual C# 中设计用户界面

其他资源

自定义、显示和打印 Windows 窗体

Visual C# 指导教程