shaik ibrahim
shaik ibrahim

Reputation: 187

keydown event Does not work on forms but does work on Datagridview in Vb.net

Keydown Event Does not work on form1 but does work on datagrid. However i want it to work on form1 only. If i comment the lines in datagrid Keydown Event The event in still form1 does not work. Can somebody help me? this is just a sample coding.The datagridview is dock in the form.

Private Sub form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
    If ((e.KeyCode = Keys.Z) AndAlso e.Control) Then
        MsgBox("hello")
    End If
End Sub


Private Sub DataGrid_KeyDown(sender As Object, e As KeyEventArgs) Handles DataGrid.KeyDown
    If ((e.KeyCode = Keys.Z) AndAlso e.Control) Then
        MsgBox("hello")
    End If
End Sub

Upvotes: 0

Views: 1468

Answers (1)

SysDragon
SysDragon

Reputation: 9878

Change the .KeyPreview property of the form to True.

When this property is set to true, the form will receive all KeyPress, KeyDown, and KeyUp events.

More information in MSDN Documentation.

Upvotes: 3

Related Questions