2015-01-21 65 views
0

如何防止在keydown事件中通過alt + f4鍵關閉窗體?我面臨的問題是保持按住Alt鍵並按F4關閉窗體,但我想阻止窗體關閉。下面是我的代碼在關鍵事件中禁用Alt + F4功能

Private Sub frminstituteselect_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown 
    If e.Alt = True And e.KeyCode = Keys.F4 Then 
     e.Handled = True 
    End If 
End Sub 

回答

0

嘗試這樣

Private ALT_F4 As Boolean = False 


Private Sub frminstituteselect_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Form1.FormClosing 
    If ALT_F4 Then 
     e.Cancel = True 
     Return 
    End If 
End Sub 


Private Sub frminstituteselect_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Form1.KeyDown 
    ALT_F4 = (e.KeyCode.Equals(Keys.F4) AndAlso e.Alt = True) 
End Sub 

或者乾脆

Private Sub frminstituteselect_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Form1.FormClosing 
      e.Cancel = True 
    End Sub 
+0

感謝您的答覆。但我不想在表單關閉事件上使用。讓我解釋一下,我有一個表單和一個按鈕,單擊按鈕時,表單應該被關閉,但不是在alt + f4上,也不在表單關閉事件中。還有一件事我想說的是窗體將通過保持按住Alt鍵並按下F4來關閉,這個我不需要。 – Sachin 2015-01-21 09:29:07

+0

如果單獨按下ALT鍵和F4鍵,則您提到的代碼將起作用。 – Sachin 2015-01-21 09:30:32