[RESOLVED] [2005] keyboard events
Hello, i am not a begginer programmer but i never have the need to use keyboard events and when i learned a bit after i forgot how. I want to make a if statement that does this: When the user click left arrow key move picture box to the left. I basicly only need the code for the if left arrow key is pressed. I do not want to use a case i know there is another way besides using select case, since when i learned i didn't use that method.
Re: [2005] keyboard events
You need to handle the appropriate controls KeyDown event, I assume you'll be needing the Forms KeyDown event:
vb Code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Left Then
'Do whatever
End If
End Sub
Re: [2005] keyboard events
Thankyou that is the Exact code i wanted thankyou very much because whenever i asked someone else they always used select cases and i dont get the concept of them.
Re: [2005] keyboard events
Using Select Case provides an advantage of readability versus having many If-statements. See here:
vb Code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Left Then
'Do whatever
ElseIf e.KeyCode = Keys.Right
'Do something else
ElseIf e.KeyCode = Keys.Up
'Do something crazy
End If
End Sub
The equivallent Select Case would be:
vb 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.Left
'Do whatever
Case Keys.Right
'Do something else
Case Keys.Up
'Do something crazy
End Select
End Sub
They do the exact same thing, but the latter results in less code and is easier to read.
Re: [2005] keyboard events
oh whell then thankyou again i think i am going to use a select case, but where u wrote select case e.keycode then you jsut wrote case keys.left case keys.right where in if statements u have to use e.keycode = keys.left etc.. so where u wrote in select case e.keycode that determines???
Re: [2005] keyboard events
Quote:
Originally Posted by noahssite
oh whell then thankyou again i think i am going to use a select case, but where u wrote select case e.keycode then you jsut wrote case keys.left case keys.right where in if statements u have to use e.keycode = keys.left etc.. so where u wrote in select case e.keycode that determines???
http://msdn2.microsoft.com/en-us/lib...hx(VS.71).aspx;)
Re: [2005] keyboard events
I understand now thankyou...
Re: [RESOLVED] [2005] keyboard events