Brian Ward wrote:
Hello
=====
I am trying to do a simple exercise using Visual Studio C#.
I have a form with one picturebox and some buttons.
I want to be able to control a picture box graphic using the keyboard
(especially arrow keys). All works well by using the Form1_KeyDown()
method .. until I add buttons to the form .. then the keyboard control
ceases to work.
Probably because the Button controls have the focus when you are trying
use the keyboard, and so are getting the key events.
I have looked for properties to change to give the form priority but
can't see anything.
Sounds like you want the Form.KeyPreview property, set to "true". This
will cause the Form instance to receive all of the key events before
they are sent to the control that actually has focus. Set the "Handled"
property in the appropriate event args class if you actually handle the
key event, so that you don't get parallel handling of the key (ie the
form reacts to a key and some other control does at the same time).
I don't believe that your issue has anything to do with using WPF or
not. I can't imagine that given your specific question, rewriting your
project to use WPF just so that the form can get key events is an
appropriate solution.
Pete