No overload for 'insert_method_here' matches delegate 'System.EventHandler'

Cistoran picture Cistoran · Aug 27, 2010 · Viewed 27.4k times · Source

I am trying to build a program that has a button, and everytime that button is clicked, it moves the button and adds to the score. However, I am trying to disable the Enter key, or suppress the command when pressed. Here is what I have so far

private void button1_Click(object sender, EventArgs e, KeyEventArgs k)
    {
        if (k.KeyCode == Keys.Enter)
        {
            k.SuppressKeyPress = true;
        }
        score = score + 10;
        timesClicked++;
        int rand1 = RandomNumber(1, 400);
        int rand2 = RandomNumber(1, 400);
        button1.Location = new Point(rand1, rand2);
        toolStripScore.Text = ("Your score is " + score);
        toolStripClicks.Text = ("You've clicked the button{0} times " + timesClicked);
        winCheck();
    }

This is what I added to prevent the enter key from going in.

if (k.KeyCode == Keys.Enter) { k.SuppressKeyPress = true; }

However it generates the error... "No overload for 'button1_Click' matches delegate 'System.EventHandler'" And when clicked to show the location, it opens the code for Form1.Designer and points to this this line. "this.button1.Click += new System.EventHandler(this.button1_Click);"

Any help on resolving this issue would be much appreciated.

Answer

max picture max · Aug 27, 2010

Your method signature does not match EventHandler delegate (that is, you cannot just add KeyEventArgs argument and get this working). You'll need to handle more than one event to do what you want (look at KeyDown or KeyPress events).

Alternatively, use MouseClick event instead of Click event.