0

I have downloaded a dll for a custom base Winforms as my base form, the developer of the custom form is no longer available and all of my forms inherits on this base form as it has some functionalities common to all of my other forms, my problem here is that the event when I press the ESC key exits or closes the active form immediately without a warning, I wanted to override the keydown event of the ESC key but it does not work. I tried this code below but they are not working, When I press ESC the form closes since the ESC Event which closes the form is embedded in the custom base form.

  Private Sub frmMain_KeyPress(sender As Object, e As KeyPressEventArgs)
        If e.KeyChar = Chr(27) Then
            e.Handled = True
        End If
    End Sub


    Private Sub frmMain_KeyDown(sender As Object, e As KeyEventArgs)
        If e.KeyCode = Keys.Escape Then
            e.Handled = True
        End If
    End Sub
E-Bat
  • 4,792
  • 1
  • 33
  • 58
  • You can try to remove all event handlers from the `KeyDown` event via [reflection shown in this answer](http://stackoverflow.com/a/91853/2882256). – Alex B. Apr 23 '17 at 15:54
  • Thanks for the reply, I cannot possibly remove the Keypress event because I need it on the form, I just wanted to cancel the ESC key event from the Base form so that the form will not exit because it is the default behavior on the base form – Renoir Rapido Apr 23 '17 at 16:33

2 Answers2

0

try to use overrides event, Overriding Event Handlers with Visual Basic .NET

0

It seems that your base form has the property KeyPreview set to True. You need to set it to false, probably the best place will be at the end of your constructor.

'frmMain.vb
Public Sub New()
    InitializeComponent()     
    MyBase.KeyPreview = False
End Sub

More information on KeyPreview property here

E-Bat
  • 4,792
  • 1
  • 33
  • 58
  • If I am going to set key preview to false, I cannot use anymore Funtion keys on my form, is there a workaround so that I can use any key when the keypreview is set to false? Thanks! – Renoir Rapido Apr 23 '17 at 22:28
  • Well, if the api of your base class don't let you intercept and handle this situations I don't know of any workaround on it. I'll probably set KeyPreview = False and handle key event on derived forms. – E-Bat Apr 24 '17 at 01:16