VB.net - using buttons

Soldato
Joined
8 Oct 2005
Posts
4,184
Location
Midlands, UK
Hi,

This is driving me crazy. I need to move a graphics object around a picture box, using the left,right,up and down keys. As far as i know you use the below method to achieve that:

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 something
			Case Keys.Right
				'Etc  
			Case Keys.Up
				 'and something else
			Case Keys.Down
				 'flsdfs

		End Select

I have even tried adding some code to display a simple message box for each case (to check if it works that way), to no no avail.

Am i missing something here?

thx
 
Inquisitor said:
I think the problem is that the KeyDown event is only raised when Form1 itself actually has focus (as opposed to the controls within it). I've had this problem when trying to create hot keys in my application, and had to resort to API calls to get them to work properly regardless of the which control had focus at the time.

Have a look at these API functions:

http://www.pinvoke.net/default.aspx/user32/RegisterHotKey.html
http://msdn2.microsoft.com/en-us/library/ms646309.aspx

http://www.pinvoke.net/default.aspx/user32/UnregisterHotKey.html
http://msdn2.microsoft.com/en-us/library/ms646327.aspx

It's a bit messy really as you have to specify IDs and check them when the form receives a Windows message, etc., but it's the most versatile way of doing it I find.

I'm not asure focus is the problem, as the form only has a button and a picturebox. I click the button and after it disableas, so there is only the picturebox left.
 
Inquisitor said:
Try it on another form with no controls and it should work, which implies that it's the controls that are taking focus away from the form :)

ah your right, twas miles away :)
 
Back
Top Bottom