Cannot move form around with arrow keys

EagleEye

Member
Joined
Nov 13, 2015
Messages
13
Programming Experience
Beginner
I'm trying to move my form around by using the left, right, up & down key on the keyboard, but nothing happens.

Here is the code i'm trying:
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);
        }
    }
}
 
Last edited:
Problem solved!

This is how the code should look like:

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);
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Up)
            {
                Top -= 1;
                return true;
            }
            if (keyData == Keys.Down)
            {
                Top += 1;
                return true;
            }
            if (keyData == Keys.Left)
            {
                Left -= 1;
            }
            if (keyData == Keys.Right)
            {
                Left += 1;
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
 
Last edited by a moderator:
I'm trying to move my form around by using the left, right, up & down key on the keyboard, but nothing happens.

Here is the code i'm trying:
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);
        }
    }
}
You're not handling the form's KeyUp event, which is where you would check for the 4 arrow keys and move the form appropriately. Just be sure to set the form's KeyPreview to true.
namespace ArrowKeysForm {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void upButton_Click(object sender, EventArgs e) {
            this.Top -= 10;
        }

        private void downButton_Click(object sender, EventArgs e) {
            this.Top += 10;
        }

        private void leftButton_Click(object sender, EventArgs e) {
            this.Left -= 10;
        }

        private void rightButton_Click(object sender, EventArgs e) {
            this.Left += 10;
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e) {
            switch (e.KeyCode) {
                case Keys.Up:
                    upButton.PerformClick();
                    break;
                case Keys.Down:
                    downButton.PerformClick();
                    break;
                case Keys.Left:
                    leftButton.PerformClick();
                    break;
                case Keys.Right:
                    rightButton.PerformClick();
                    break;
            }
        }
    }
}
 
You're not handling the form's KeyUp event, which is where you would check for the 4 arrow keys and move the form appropriately. Just be sure to set the form's KeyPreview to true.
namespace ArrowKeysForm {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void upButton_Click(object sender, EventArgs e) {
            this.Top -= 10;
        }

        private void downButton_Click(object sender, EventArgs e) {
            this.Top += 10;
        }

        private void leftButton_Click(object sender, EventArgs e) {
            this.Left -= 10;
        }

        private void rightButton_Click(object sender, EventArgs e) {
            this.Left += 10;
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e) {
            switch (e.KeyCode) {
                case Keys.Up:
                    upButton.PerformClick();
                    break;
                case Keys.Down:
                    downButton.PerformClick();
                    break;
                case Keys.Left:
                    leftButton.PerformClick();
                    break;
                case Keys.Right:
                    rightButton.PerformClick();
                    break;
            }
        }
    }
}

Thank you JuggaloBrotha,

I will add that tomorrow.
 
Back
Top Bottom