如何:从一个窗体显示另一个窗体 (Visual C#)

更新:2007 年 11 月

本示例从一个 Windows 窗体显示第二个窗体。

本示例需要两个分别名为 Form1 和 Form2 的 Windows 窗体。

  • Form1 包含一个名为 button1 的 Button 控件。

过程

创建 Form1

  1. 创建一个 Windows 窗体应用程序,并将其命名为 Form1。

  2. Button 控件添加到窗体中,并将其命名为 button1。

  3. 将 Form2 类添加到命名空间中,并如以下代码所示设置 button1 的 Click 事件处理程序。

    当单击该按钮时,Form2 将显示。

示例

private void button1_Click(object sender, System.EventArgs e)
{
    Form2 frm = new Form2();
    frm.Show();
}
// Create Form2.
public class Form2: Form
{
    public Form2()
    {
        Text = "Form2";
    }
}

请参见

概念

在 Visual C# 中设计用户界面

其他资源

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

Visual C# 指导教程