Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB KeyDown Event Problem

Status
Not open for further replies.

Vibeguy2

Programmer
Joined
Jan 16, 2006
Messages
2
Location
US
I've included the following code into a program I'm working on under the form1 class, and it's not hitting the event whenever I press a key. I tried putting a debug stop on the line where the event is called, and when I push a key while the program's running, nothing happens. Could anyone tell me what could make this happen? I tried making sure the focus was on the form by setting Form1.KeyPreview = true and it didn't help. Any ideas would be appreciated:

Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Select Case e.KeyCode
Case Keys.Right
BItem.Right()
Case Keys.Left
BItem.Left()
Case Keys.Up
BItem.Up()
Case Keys.Down
BItem.Down()
End Select
End Sub
 
Have you tried 'on key pressed' property of the form or textboxes?


Ian Mayor (UK)
Program Error
Programmers do it one finger at a time!
 
Yeah, tried that. So far all that I can figure out is that it's related to the button I have on my form. When I include a "Button1.Dispose()" in the button click event handler, then the KeyDown event works fine, but I'd like to be able to use this program without destroying the button as soon as it's pressed.
 
Vibeguy2,
First of all this forum is dedicated to MS Access Forms. Your question is for VB.Net; and that has a special forum in Tek-Tips. (forum796)

Anyhow here is the code to handle it. Remember to set form's Key Preview=True
Code:
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.Right
                MessageBox.Show("You pressed right button")
            Case Keys.Left
                MessageBox.Show("You pressed left button")
            Case Keys.Down
                MessageBox.Show("You presseddown button")
            Case Keys.Up
                MessageBox.Show("You pressed up button")
        End Select
    End Sub


________________________________________________________
Zameer Abdulla
Help to find Missing people
Education's purpose is to replace an empty mind with an open one.
~Malcolm S. Forbes~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top