-1

I have been trying to teach myself c# through tutorials and this website and I didn't know how to make my "character" move using the arrow keys so I copied code from here hoping it would work and everything is fine until I run and then it throws me this error

Error 1 No overload for 'pictureBox1_Click' matches delegate 'System.EventHandler' c:\users\collin\documents\visual studio 2013\projects\my_rpg\my_rpg\form1.designer.cs 80 39 My_RPG

Here is the code I copied (I did change the name from whatever it was originally to pictureBox1 so that is correct"

public MainScreen()
    {
        InitializeComponent();
        KeyDown += new KeyEventHandler(MainScreen_KeyDown);
        if (characterCreated == false)
        {
            playGameBtn.ForeColor = Color.Gray;

        }
    }
private void pictureBox1_Click(object sender, KeyEventArgs e)
    {
        int x = pictureBox1.Location.X;
        int y = pictureBox1.Location.Y;

        if (e.KeyCode == Keys.Right)
        {
            x += 2;
        }
        else if (e.KeyCode == Keys.Left) 
        {
            x -= 2;
        }
        else if (e.KeyCode == Keys.Up)
        {
            y += 2;
        }
        else if (e.KeyCode == Keys.Down)
        {
            y -= 2;
        }

        pictureBox1.Location = new System.Drawing.Point(x, y);

    }

And then if I click on the error it sends me to this line of code that if I edit then it throws me errors

        this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
Russ Perlow
  • 7
  • 1
  • 5
  • As this error states, you are trying to create an event handler using a function whose signature does not match the requirements. Your function would match a keyeventhandler instead. However that is not what the `.Click` expects, so you probably will have to attach that event handler to you main window instead – UnholySheep Sep 30 '15 at 20:46
  • Trying to get keyboard event arguments out of a mouse event ought to make you go, hmm, does that really make sense? – Hans Passant Sep 30 '15 at 22:53

1 Answers1

1

The signature for the PictureBox Click event wants a method that receives an object and an EventArgs parameter.

Your code declares the pictureBox_Click method with an object and a KeyEventArgs. Of course the compiler is not happy and tells you that something is not as it should be.

From your code, it seems that you want to handle the KeyDown event at the form level, so you need to change your code to have a method to handle the KeyDown event and restore the original signature for the picture box

public MainScreen()
{
    InitializeComponent();
    KeyDown += new KeyEventHandler(MainScreen_KeyDown);
    if (characterCreated == false)
    {
        playGameBtn.ForeColor = Color.Gray;

    }
}
private void pictureBox1_Click(object sender, EventArgs e)
{
     // REMOVE ALL THE CODE FROM THIS EVENT AND MOVE IT TO MainScreen_KeyDown  event
}
private void MainScreen_KeyDown(object sender, KeyEventArgs e)
{

    int x = pictureBox1.Location.X;
    int y = pictureBox1.Location.Y;

    if (e.KeyCode == Keys.Right)
    {
        x += 2;
    }
    else if (e.KeyCode == Keys.Left) 
    {
        x -= 2;
    }
    else if (e.KeyCode == Keys.Up)
    {
        y += 2;
    }
    else if (e.KeyCode == Keys.Down)
    {
        y -= 2;
    }

    pictureBox1.Location = new System.Drawing.Point(x, y);
}

Also, keep in mind, that to receive KeyDown events at the Form level you need to set the Form.KeyPreview property to true

Steve
  • 213,761
  • 22
  • 232
  • 286