我正在尝试构建一个带有窗体窗口的应用程序,该窗体窗口需要通过按一个按钮来打开和关闭另一个窗体。我希望使用同一按钮来打开和关闭同一窗口。
private void button1_Click(object sender, EventArgs e)
{
//GameBoard gameBoard = new GameBoard(); is written outside the private void as global variable.
if (gameBoard == open)
{
gameBoard Close();
}
else
{
gameBoard.Show();
}
}
感谢您的帮助。
答案 0 :(得分:0)
首先将表单引用设置为null。
然后您可以执行以下操作:
private GameBoard gameBoard = null;
private void button1_Click(object sender, EventArgs e)
{
if (gameBoard == null || gameBoard.IsDisposed)
{
gameBoard = new GameBoard();
gameBoard.Show();
}
else
{
gameBoard.Close();
gameBoard = null;
}
}