[英]Must click “X” on MDIParent form multiple times to close application, each click closes MDIChild
我已經開始制作C#MDI應用程序的原型,並且遇到了問題。 看來,當在MDIParent中打開MDIChild時,我必須多次按下父級上的關閉按鈕才能關閉應用程序。 每次單擊關閉按鈕都會關閉一個MDIChildren。
我懷疑這與我的MDIChildren的基本表單的close方法有關。
private void _AssetFormBase_FormClosing(object sender, FormClosingEventArgs e)
{
if(sender != this.MdiParent)
{
e.Cancel = true;
this.Hide();
}
}
雖然我的上述技巧似乎無效。 我假設當MDIParents close調用時,它依次首先調用其所有子級的close方法。 因此,如果發件人是父母,那么我不會取消並隱藏(以保留表單狀態),而不會這樣做並允許通常發生的任何事情。
知道可能是什么問題嗎?
發件人不是您想的那樣。 改用e.CloseReason,您將獲得CloseReason.MdiFormClosing。 但是,請勿測試該特定值,也不要阻止操作系統關閉。 采用:
private void _AssetFormBase_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.Hide();
}
}
請注意,在您自己的代碼中調用Close()時,也會獲得UserClosing。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.