Question KeyPreview stops work after some control is Disabled

Budius

Well-known member
Joined
Aug 6, 2010
Messages
137
Location
UK
Programming Experience
3-5
hi guys,

It's a serial communication software, so what happens is:

the user clicks a button, I set the HourGlass, disable the button (to avoid ppl that are too used with FPS games), do the serial communication bit that last a couple of seconds, then the set the mouse back to normal and re-enable the button.
And I have a few more like those through the software using text boxes or drop-down menus.

the skeleton code is:
VB.NET:
        Me.Cursor = Cursors.WaitCursor
        Toggle.Enabled = False

        ' send communication bits
        ' receive the communication back
        ' update the received data

        Me.Cursor = Cursors.Default
        Toggle.Enabled = True

Sounds simple BUT, after the control been disabled and re-enabled the following routine stops been called:

VB.NET:
Private Sub frm_Setup_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If Asc(e.KeyChar) = 27 Then Me.Hide()
    End Sub
My form is set in design time to be KeyPreview = True and I even tried to set it again after the Click. The routine is simply not called and the ESC key on the keyboard does not hide the form anymore.

Any ideas ???
 
I've changed the event to Me.KeyUp and now it seems to be working.... but the question remains... WHY ???

VB.NET:
    Private Sub frm_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode.ToString = "Escape" Then Me.Hide()
    End Sub
 
Back
Top