|
-
Mar 15th, 2008, 08:12 AM
#1
Thread Starter
Frenzied Member
[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.
-
Mar 15th, 2008, 08:26 AM
#2
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
-
Mar 15th, 2008, 08:29 AM
#3
Thread Starter
Frenzied Member
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.
-
Mar 15th, 2008, 08:34 AM
#4
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.
-
Mar 15th, 2008, 08:47 AM
#5
Thread Starter
Frenzied Member
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???
-
Mar 15th, 2008, 08:51 AM
#6
Re: [2005] keyboard events
 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
-
Mar 15th, 2008, 12:45 PM
#7
Thread Starter
Frenzied Member
Re: [2005] keyboard events
I understand now thankyou...
-
Mar 15th, 2008, 05:54 PM
#8
Thread Starter
Frenzied Member
Re: [RESOLVED] [2005] keyboard events
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|