Question Moving an image in a picture box with arrow keys?

JDTrakal

New member
Joined
Oct 28, 2010
Messages
3
Programming Experience
Beginner
Hi, this is my first time here so I apologise if I've placed this in the wrong area.

I'm currently working on a project for university, basically we were given an incomplete program to finish off with bonus points for adding addition features in.

Part of the program is to move an image (a monkey) around the picture box (pen). Now to move the monkey we use 4 buttons on the form called Up, Down, Left and Right, however as we need to move the monkey around to test other parts of the program it gets annoying having to click on the button.

As such I was able to get some code which allows me to move the monkey around using the arrow keys (I think you can all see the benefit of this). When I use the code in a blank form it works fine, however when I add a button onto the form the arrow keys will instead highlight the buttons.

I have figure our a way around this, basically set up a button to disable all buttons on the form until a certain key is pressed, however I would prefer not to have to do this.

Can any one help me with this please?

Thanks
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,617
Location
Norway
Programming Experience
10+
The form captures the KeyDown event for arrow keys when it contains controls that take focus. You can still use the KeyUp event for arrow keys, but that will include the default form behaviour and also not allow handling repeated key input by KeyDown event. Another option is to override ProcessDialogKey method where you handle these keys and return True for them if you don't want them further processed.
 

JDTrakal

New member
Joined
Oct 28, 2010
Messages
3
Programming Experience
Beginner
Thanks very much, got it working. Just one more question however when I hold down an arrow key the program will move the button selector around, is there anyway to stop this?

Apologies if this query has been answered however I can't find it in the link provided.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,617
Location
Norway
Programming Experience
10+
Like I said, you must return True from ProcessDialogKey method to prevent further processing of those keys.
 
Top Bottom