Inzi Irina
Inzi Irina

Reputation: 267

How to move a control in a form with by keyboard?

How can I move a control ( a image ) in the form by keyboard? I don't know how to do it. C# Thanks

Upvotes: 0

Views: 966

Answers (2)

Johnny_D
Johnny_D

Reputation: 4652

If you mean moving objects in design mode, it's impossible, because positioning of elements on form should be done via CSS.

Upvotes: 0

Christoph Fink
Christoph Fink

Reputation: 23113

Assuming you use WinForm (but for WPF its not much different):
Each control has a KeyDown event, which is fired when a key is pressed down (and a KeyUP and KeyPress which are fired accordingly:

So you can do something like the following (e.g. in the constructor or load event of the form):

//this enables the form to receive all key events if a child control has focus
this.KeyPreview = true; 

this.KeyDown += (s, e) =>
{
    if(e.KeyCode == Keys.Up)
        picture.Location.Y++;
    //etc...
}

Upvotes: 2

Related Questions