How to move a picture left or right with keystrokes

seanvoth

Well-known member
Joined
Aug 5, 2006
Messages
58
Programming Experience
1-3
ok i have a pic and when i press the left and right key i want the pic to move left and right

BUT....... in the code window where you put code in
when i look threw the Declarations on the top of the screen
it dose not say keydown so can i not make a pic move left and right

i hope you get what im trying to say:confused: :confused: :confused: :confused:
 
use the form's keydown event and if the e.keycode = keys.left then picturebox.left -= 1 and if e.keycode = keys.right then picturebox.left += 1

it's pretty straight forward
 
you also need to set the form's KeyPreview property to true, otherwise the form will ingnore the keystrokes, and the control that has focus will get them.
 
you also need to set the form's KeyPreview property to true, otherwise the form will ingnore the keystrokes, and the control that has focus will get them.

only if you use the KeyPress event, KeyUp and KeyDown don't need KeyPreview set to True
 
only if you use the KeyPress event, KeyUp and KeyDown don't need KeyPreview set to True
Not what the documentation says, also not what I see (in .Net 2.0).
 
Now I tried also with .Net 1.1, but no, I get no keyboard events for form unless I set KeyPreview True. There is only one exception as I can find:
If a form has no visible or enabled controls, it automatically receives all keyboard events.
 
interesting, in VS 2003 i've got a few projects that make use of the keyup event for moving controls on the form and i never set keypreview to true and the keydown and keyup events work fine, it's always the keypress event that needs to have keypreview set to true to work

this is true before and after installing vs2003 sp1 (and before and after .net 1.1 sp1 too)

i've never even read the documentation on this either, i've always assumed it worked this way for everyone using vs2003, i guess it's just me (cause i just tested it again on my pc, and that's how it works)

weird huh
 
Back
Top