|
-
Mar 31st, 2009, 04:51 PM
#1
Thread Starter
Member
[RESOLVED] Can't detect keypresses when a button is present
I've started building a small game where you move around a PictureBox control with keystrokes using the arrow keys. Everything was working fine....until I added a button.
Now the button steals the focus so that every time I hit an arrow key hoping to move my image, it just selects the button.
Is there any way to make the button selectable only by mouse click??
Thanks in advance for any help!!!
BTW - I'm using VB and VS2008.
-
Mar 31st, 2009, 05:08 PM
#2
Re: Can't detect keypresses when a button is present
I dont completely understand your problem, but do you use a Key event to determine what the arrow keys do? If so, you should be able to override that behaviour. What is your KeyDown/Up/Press code?
If I do this for example on a form with a myriad of controls, I dont lose the function of the keys;
Code:
Private Sub Form1_KeyDown(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles MyBase.KeyDown
'Note: I have Me.KeyPreview = True
Select Case e.KeyCode
Case Keys.Left : PictureBox1.Left -= 1
Case Keys.Right : PictureBox1.Left += 1
Case Keys.Up : PictureBox1.Top -= 1
Case Keys.Down : PictureBox1.Top += 1
End Select
End Sub
Last edited by Bulldog; Mar 31st, 2009 at 05:16 PM.
-
Mar 31st, 2009, 05:24 PM
#3
Re: Can't detect keypresses when a button is present
You can't make the button not focusable, but if you set your Forms KeyPreview property to True, then the your Form1 KeyDown event will still work even if the button has the focus.
-
Mar 31st, 2009, 05:27 PM
#4
Thread Starter
Member
Re: Can't detect keypresses when a button is present
That's exactly how mine is coded and it works when the button control is not on the form. As soon as I add the button though, the key down event never gets handled.
When the form loads, the button is not selected, but when I hit one of the arrow keys, the button becomes selected and is highlighted. The arrow keys are acting almost like a Tab key press - wanting to cycle through the button controls. In fact, I added a second button and every time I hit the right arrow, it selects the other button.
I've tried every property I thought would work, and no luck... It seems like there would be a simple answer to this, but I'm not finding it.
-
Mar 31st, 2009, 05:31 PM
#5
Thread Starter
Member
Re: Can't detect keypresses when a button is present
Thanks Vectris - I just saw your post and tested that, but unfortunately it still doesn't work.
To make things simple, I started a new project, added 2 buttons to the form, and then set the form KeyDown event to cause a message box to pop up when the right arrow key is pressed. Unfortunately, even with the form KeyPreview property set to True, pressing the right arrow key just cycles through the 2 buttons.
-
Mar 31st, 2009, 06:34 PM
#6
Re: Can't detect keypresses when a button is present
I can reproduce that problem, but this seems to work as a fix;
Code:
Protected Overrides Function ProcessCmdKey( _
ByRef msg As System.Windows.Forms.Message, _
ByVal keyData As System.Windows.Forms.Keys) As Boolean
Select Case keyData
Case Keys.Left
PictureBox1.Left -= 1
Case Keys.Right
PictureBox1.Left += 1
Case Keys.Up
PictureBox1.Top -= 1
Case Keys.Down
PictureBox1.Top += 1
End Select
PictureBox1.Focus()
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
-
Mar 31st, 2009, 06:53 PM
#7
Re: Can't detect keypresses when a button is present
Just turn the TabStop property of all your buttons to False and keep KeyPreview on. It worked for me with 5 buttons, no coding necessary .
-
Mar 31st, 2009, 07:04 PM
#8
Re: Can't detect keypresses when a button is present
Didnt work for me. I created a new project as the OP described and get exactly the same problem. Setting tabstops didnt help.
This was my test code, I had the PictureBox and two buttons on the form;
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyDown(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles MyBase.KeyDown
Select Case e.KeyCode
Case Keys.Left : PictureBox1.Left -= 1
Case Keys.Right : PictureBox1.Left += 1
Case Keys.Up : PictureBox1.Top -= 1
Case Keys.Down : PictureBox1.Top += 1
End Select
End Sub
End Class
This code will work if I add a TextBox.
If I remove the TextBox, I can only fix it using this code instead of the KeyDown event;
Code:
Protected Overrides Function ProcessCmdKey( _
ByRef msg As System.Windows.Forms.Message, _
ByVal keyData As System.Windows.Forms.Keys) As Boolean
Select Case keyData
Case Keys.Left
PictureBox1.Left -= 1
Case Keys.Right
PictureBox1.Left += 1
Case Keys.Up
PictureBox1.Top -= 1
Case Keys.Down
PictureBox1.Top += 1
End Select
PictureBox1.Focus()
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
-
Mar 31st, 2009, 07:40 PM
#9
Re: Can't detect keypresses when a button is present
Ya I had a textbox on my form, I guess that messed it up.
There is one other way though, don't use buttons. Instead make labels and you can even make them look better by customizing the MouseOver backcolor etc. That way would be harder than BullDogs though, so his is probably the best.
-
Apr 1st, 2009, 10:44 AM
#10
Thread Starter
Member
Re: Can't detect keypresses when a button is present
Thanks Bulldog - I just tested your solution out and it works great! I would've never gotten that on my own. I appreciate it!
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
|