Ohad's Blog

Lets talk about .net !

Mirror at:
blogs.microsoft.co.il

News

         Ohad Israeli's Facebook profile

Grab this badge here!

C# Code Snippts

Favorite Blogs

Israeli .Net Bloggers

Moving a winform using the arrow keys

A question that I've received today was how to move a winform using the keyboard arrow keys

Its pretty simple... you need to override the ProcessCmdKey.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)

{

    //Returns true if the character was processed by the control;

    //otherwise, false.

    bool bHandled = false;

 

    switch (keyData)

    {

        case Keys.Right:

            this.Left += 10;

            bHandled = true;

            break;

        case Keys.Left:

            this.Left -= 10;

            bHandled = true;

            break;

        case Keys.Up:

            this.Top -= 10;

            bHandled = true;

            break;

        case Keys.Down:

            this.Top += 10;

            bHandled = true;

            break;

    }

    return bHandled;

}

 

Comments

Fabrice Marguerie said:

Well, it's even simpler than that: you can simply use the "Move" command from the system menu. (ALT+SPACE, M, arrows).
# June 6, 2006 5:36 AM

Ohad Israeli said:

For sure you can use the Move command from the system menu... the question i've recived was how to capture the arrow keys using c# :-)
# June 6, 2006 4:55 PM

Euan said:

Ah thats a sweet snippet of code.

I can definately use the key override. Not sure about moving the window around but the concept is great.

Nice work.

Euan

# November 16, 2006 4:28 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)