[英]C# - How Do I Close Main Form Together With Child Forms (while child windows must close ONLY when main form is closing)
我有一个主窗口和几个子窗口。 子窗口包含一些数据,因此我不希望它们被关闭。 所以我在FormClosing中将它们隐藏起来,将e.Cancel = true;
设置e.Cancel = true;
但是当我要关闭主窗口时(退出应用程序)。 我有一些麻烦要退出它。 首先,它不会关闭。 但是要注意,我要关闭一个额外的变量,我实际上需要2次单击才能关闭它(问题在执行onFormClosing(mainWIndow, ...)
之前onFormClosing(mainWIndow, ...)
框架尝试关闭将首先隐藏自身的子项)。
// main and child windows have this as form closing event proc
private void onFormClosing(object sender, FormClosingEventArgs e)
{
if (sender == this) // main
{
exitApp = true;
}
else
if (sender == formChild) // child
{
if (!exitApp)
{
e.Cancel = true;
formChild.Hide();
}
}
}
如何一键关闭它们?
我也想避免Application.Exit();
因为我需要确保所有线程都正确关闭。 所以最好是干净的关闭。
您可以使用Application.Exit()强制应用程序。
自己找到解决方案:
// ...
if (sender == this) // main
{
exitApp = true;
FormClosing -= onFormClosing;
Close();
}
// ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.