Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I just want to move a form around on the screen with the arrows on the keyboard.

What I have tried:

First i tried the lines i have comment out and then i have tried the other code below, but nothing happens with any of the solution i have tried.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace SC1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left) { this.Left += e.X - lastPoint.X; this.Top += e.Y - lastPoint.Y; }
        }

        Point lastPoint;
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            lastPoint = new Point(e.X, e.Y);
        }

        //Move form to the Right
        //private void RightButton_Click(object sender, EventArgs e)
        //{
        //    this.Location = new Point(this.Location.X + 10, this.Location.Y);
        //}
        
        //Move form to the left:
        //private void LeftButton_Click(object sender, EventArgs e)
        //{
        //     this.Location = new Point(this.Location.X - 10, this.Location.Y);
        //}
  
        //Move form up:
        //private void UpButton_Click(object sender, EventArgs e)
        //{
        //     this.Location = new Point(this.Location.X, this.Location.Y - 10);
        //}  

        //Move form down:
        //private void DownButton_Click(object sender, EventArgs e)
        //{
        //    this.Location = new Point(this.Location.X, this.Location.Y + 10);
        //}

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            //capture up arrow key
            if (keyData == Keys.Up)
            {
                MessageBox.Show("You pressed Up arrow key");
                return true;
            }
            //capture down arrow key
            if (keyData == Keys.Down)
            {
                MessageBox.Show("You pressed Down arrow key");
                return true;
            }
            //capture left arrow key
            if (keyData == Keys.Left)
            {
                MessageBox.Show("You pressed Left arrow key");
                return true;
            }
            //capture right arrow key
            if (keyData == Keys.Right)
            {
                MessageBox.Show("You pressed Right arrow key");
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
Posted
Updated 1-Jul-17 4:17am

1 solution

Try:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
    if (keyData == Keys.Up)
        {
        Top -= 20;
        return true;
        }
    if (keyData == Keys.Down)
        {
        Top += 20;
        return true;
        }
    if (keyData == Keys.Left)
        {
        Left -= 20;
        }
    if (keyData == Keys.Right)
        {
        Left += 20;
        return true;
        }
    return base.ProcessCmdKey(ref msg, keyData);
    }
 
Share this answer
 
Comments
OriginalGriff 1-Jul-17 10:43am    
If works fine for me!

Are you sure it's showing the right form?
Does it have the focus?

Stupid question, I know, but... did it compile without errors or warnings?
georgebaker 1-Jul-17 10:49am    
It works perfectly. It's me doing the wrong thing.

I used the buttons on the form and not the keyboard keys to test it. Stupid i know ;-)
OriginalGriff 1-Jul-17 11:01am    
We all have days like that! (Sometimes, I have whole *weeks*! :blush:)
georgebaker 1-Jul-17 13:45pm    
Ha ha LOL. Yeah :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900