0

I tried searching all over the site but couldn't really find an answer to this question, I'm trying to make my Ellipse move by using the arrowkeys, but I keep getting the same error whatever I try.

Also, I get this error when I put this in my Canvas code in the XAML

KeyDown="movement"

Error:

No overload for 'movement' matches delegate 'System.Windows.Input.KeyEventHandler'

Code:

    public MainWindow()
        {
            InitializeComponent();
            x = 10;
            y = 10;
            diameter = 10;
            DrawBall(x, y, diameter);
        }

        private void movement(object sender, System.Windows.Forms.KeyEventArgs key)
        {
            if (key.KeyCode == Keys.Up)
            {
                x -= 20;
            }
            else if (key.KeyCode == Keys.Down)
            {       
                x += 20;
            }
           else if (key.KeyCode == Keys.Left)
            {
                y -= 20;
            }
           else if (key.KeyCode == Keys.Right)
            {
                y += 20;
            }
          DrawBall(x, y, diameter);
        }

             private void DrawBall(double x, double y, double diameter)
        {
            ballCanvas.Children.Clear();
            Ellipse ellipse = new Ellipse();
            ellipse.Stroke = new SolidColorBrush(Colors.Black);
            ellipse.Fill = new SolidColorBrush(Colors.Red);
            ellipse.Width = diameter;
            ellipse.Height = diameter;
            ellipse.Margin = new Thickness(x, y, 0, 0);
            ballCanvas.Children.Add(ellipse);

        }   
    }
}
pcs
  • 1,864
  • 4
  • 25
  • 49

1 Answers1

1

You are using KeyEventArgs from Windows Forms. Instead use:

..., System.Windows.Input.KeyEventArgs key)...
James Lucas
  • 2,452
  • 10
  • 15
  • Yes, but in that case I won't be able to use KeyCode. – Daan Theunis May 05 '15 at 12:31
  • Unfortunately the compiler requires the correct type. Use the answer here to obtain the keycode: http://stackoverflow.com/questions/544141/how-to-convert-a-character-in-to-equivalent-system-windows-input-key-enum-value – James Lucas May 05 '15 at 12:33
  • Ok, I think this is a clearer answer: http://stackoverflow.com/questions/1153009/how-can-i-convert-system-windows-input-key-to-system-windows-forms-keys – James Lucas May 05 '15 at 12:45