954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Form1.KeyDown Event Problems

When using the Form1.KeyDown, keystrokes don't seem to register.

For example, the Click works fine:

Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
        MsgBox("Hello")
    End Sub


That will display the message box when the form is clicked, but if you change it to:

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        MsgBox("Hello")
    End Sub


I can't seem to get the message box to appear.

Khishin
Newbie Poster
23 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

Hmn. The code looks right. I would try to change it from handling mybase though, to handling the form in question. I'm not sure if that will make a big difference or not, since I don't use .NET, but it would seem to me that the sub should handle keypress event of the form's name.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

if you want it to work you will have to put the "keypreview" property of Form1 to true. This will make keyboard events be registered on the form.

In VB6 this same property made the form's keyboard events take priority from the other controls' keyboard events. It seems to have changed

saludos

makatzena
Newbie Poster
2 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You