0

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!

Alex V
  • 25
  • 4
  • If your sender is a button, just do `btn = sender as Button`. Now you know what button triggered the function. – Phiter Dec 09 '16 at 01:07
  • Alright, that cleared up the sender. What should i put for e? i want it to be whatever the user inputs, so that if they press an arrow key it will move the button in that direction. Using Keydown as the e value comes up as an error because it hasn't been declared. – Alex V Dec 09 '16 at 01:21
  • @AlexV do you want to call `Form1_KeyDown` manually? it will be fired every time the user presses a button on the keyboard. no need to call it manually – Mong Zhu Jan 12 '17 at 10:06

0 Answers0