-
September 8th, 2011, 07:28 PM
#1
VS 2008: Issue with keydown Space/Enter
Hi guys,
I am using vb.net within visual studio 2008.
I am using a picture box. I want to be able to use the Space or Enter key to change the image in it. For that, I try to use the keydown function as follow:
Private Sub ChestWindow_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.NumPad1
NextChest()
Case Keys.Enter
NextChest()
Case Keys.Space
NextChest()
End Select
End Sub
NextChest() is the function which changes the image. It works well when I press '1' on my numeric part of the keyboard as a test.
The issue is that when I press Space or Enter, the form containing the picturebox closes. (I've already put the keyPreview property to True)
I really do not understand. Do these keys have any default behavior? How can I do to make them doing what I want?
Thanks for your help!
Did
-
September 8th, 2011, 08:04 PM
#2
Re: VS 2008: Issue with keydown Space/Enter
-
September 8th, 2011, 10:57 PM
#3
Re: VS 2008: Issue with keydown Space/Enter
Space or enter may activate the selected button in your app.
You should add code to your forms keypreview event call your function and set e.handled=true to keep the button from being processed in another manner.
Always use [code][/code] tags when posting code.
-
September 8th, 2011, 11:55 PM
#4
Re: VS 2008: Issue with keydown Space/Enter
Hi guys,
thanks dglienna, but I already saw that before.
DataMiser, I don't totally understand what you propose me to try ?
What do you mean with "You should add code to your forms keypreview event call your function" ?
I am sorry for that, my native language is not english so sometimes it's tough for me to follow this kind of sentences. I don't have anything such as KeyPreview, except the window property, which I already set to True.
Thanks
-
September 9th, 2011, 06:05 AM
#5
Re: VS 2008: Issue with keydown Space/Enter
Sorry, When key preview is on the Form_KeyDown and Form_KeyPress events fire first to give you a preview of the key hit by the user. It is here that you need your code if that is not what you have already. If your form is named ChestWindow then your code is in the right place, if not then it needs to be moved.
In either case you need to add e.handled=true to each case to stop the key event from being passed on.
Code:
Case Keys.NumPad1
NextChest()
e.Handled=true
Case Keys.Enter
NextChest()
e.Handled=true
Case Keys.Space
NextChest()
e.Handled=true
Always use [code][/code] tags when posting code.
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
|