在按键事件中禁用Alt + F4功能

问题描述 投票:0回答:1

如何防止keydown事件中的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
vb.net
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.