2013-07-03 79 views
0

Alt + F4是關閉窗體的快捷方式。 在MDI環境中使用此快捷方式時,應用程序已關閉,因此 顯然適用於「Container」的快捷方式,而不適用於「Childform」 。如何使用Alt + F4鍵關閉子窗口?

什麼會捕捉到這個事件,並關閉活動 孩子,而不是容器

我讀到註冊Alt鍵 + F4爲熱鍵時,MDI啓動的最佳實踐。 當MDI停用時,取消註冊熱鍵。 因此,熱鍵不會影響其他窗口。

人,可以告訴有關注冊Alt鍵 + F4或更好的東西

+4

試試這個:CTRL + F4 – codingbiz

+3

不要這樣做。不要惹我的操作系統鍵盤快捷鍵。 CTRL + F4是你想要做的事情的接受捷徑。 –

+0

同意DMoses。如果你想讓你的窗口用ALT + F4關閉,不要將它設爲MDI子項。改爲創建一個頂級窗口。 –

回答

-1

你可以在你的WinForm改變void Dispose(bool disposing)方法來關閉您的孩子形式代替,就像這樣:

protected override void Dispose(bool disposing) 
{ 
    if (/* you need to close a child form */) 
    { 
     // close the child form, maybe by calling its Dispose method 
    } 
    else 
    { 
     if (disposing && (components != null)) 
     { 
      components.Dispose(); 
     } 
     base.Dispose(disposing); 
    } 
} 

編輯:正如我的評論者所說,而不是修改重寫的Dispose方法,您應該覆蓋OnFormClosing方法,而不是像這樣:

protected override void OnFormClosing(FormClosingEventArgs e) 
{ 
    if (/* you need to close the child form */) 
    { 
     e.Cancel = true; 
     // close the child form, maybe with childForm.Close(); 
    } 
    else 
     base.OnFormClosing(e); 
} 
+1

你有正確的想法,但使用'Dispose'方法是不正確的方法。改爲覆蓋'OnFormClosing',並將'e.Cancel'設置爲'true'。 –