-4

我想让 X 控件关闭窗口以隐藏当前显示的前一个表单。

在form1中我得到:

private void button1_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2();
    form2.Tag = this;
    form2.Show(this);
    Hide();
}

然后当我单击 XI 时想显示上一个并隐藏当前。

4

3 回答 3

2

您不应该Form.OnFormClosing()仅仅为此而覆盖。该Form.FormClosing事件为您提供此功能:

void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
  // Prevent the user from closing this window, minimize instead.
  if (e.CloseReason == CloseReason.UserClosing)
  {
    this.WindowState = FormWindowState.Minimized;
    e.Cancel = true;
  }
}
于 2013-01-19T22:22:22.057 回答
1

您可以覆盖OnFormClosing来执行此操作。

 protected override void OnFormClosing(FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.WindowsShutDown) return;

    // DO WHATEVER HERE
}
于 2013-01-19T21:48:10.110 回答
0

您必须跟踪您的实例化表单。

// Program.cs

public static FormA Instance;

public static void Main()
{
    Instance = new FormA();
    Instance.Show();
}

然后:

// FormB.cs

private void button1_Click(object sender, EventArgs e)
{
    Hide(); // Hide current...
    Program.Instance.Show(); // Show previous...
}
于 2013-01-19T21:52:08.380 回答