c# - Close Form 2 from Form 1 C#
问题描述
I'm trying to build an application with a form window that needs to open and close another form by pressing a button. I want the same button to be used to open and close the same window.
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();
}
}
Thanks for any help.
解决方案
首先将表单引用设置为 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;
}
}
推荐阅读
- java - 检查多个 IF 语句是否都正确,当它们不是一个动作时
- javascript - 从 html id javascript 获取值
- c# - 如何修复 Blazer 移动应用程序启动错误?
- reactjs - 为什么我的文本输入 onchange 使用 redux 工具包和 reactjs 逐个输入文本
- git - 审查委员会 - 发布单独的提交,忽略拉取提交
- sql-server - 如何将列标题从 SQL Server 复制到 Excel
- java - 即使我没有对代码进行任何更改,我的图像大小也发生了变化
- excel - 如何返回最近收到的电子邮件?
- python - 如何将参差不齐的张量与“矩形”张量连接起来?
- html - html媒体查询在iphone设备中不起作用