How to make a button move based on user input

129 Views Asked by At

I have been trying to make a button in C# move based on the user pressing various arrow keys under a Form1 KeyDown method, but i cannot figure out how to make the value of e equal to the users input. So far i have:

        private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Left)
        {
            button1.Location = new Point(button1.Top, button1.Left + 1);
        }
        else if (e.KeyCode == Keys.Right)
        {
            button1.Location = new Point(button1.Top, button1.Right + 1);
        }
        else if (e.KeyCode == Keys.Up)
        {
            button1.Location = new Point(button1.Top +1, button1.Right);
        }
        else if (e.KeyCode == Keys.Down)
        {
            button1.Location = new Point(button1.Top-1, button1.Right);
        }
    }

and then to call it I use

  Form1_KeyDown(sender: Keys.A ,e: KeyDown);

but i have no idea what to put in for the value of either sender or e. If it helps, my goal is to be able to have the user maneuver a button around the screen at a set speed. Thanks!

0

There are 0 best solutions below