“insert_method_here”没有重载匹配委托“System.EventHandler”

发布于 2024-09-16 08:24:07 字数 962 浏览 3 评论 0原文

我正在尝试构建一个具有按钮的程序,每次单击该按钮时,它都会移动按钮并添加到分数中。但是,我试图禁用 Enter 键,或在按下时抑制该命令。这是我到目前为止所拥有的

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();
    }

这是我添加的内容,以防止输入键进入。

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

但是它会生成错误...“'button1_Click'没有重载与委托'System.EventHandler'匹配”并且当单击时显示位置,它打开 Form1.Designer 的代码并指向这一行。 “this.button1.Click += new System.EventHandler(this.button1_Click);”

任何有关解决此问题的帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜妞爱困 2024-09-23 08:24:07

您的方法签名与 EventHandler 委托不匹配(也就是说,您不能仅添加 KeyEventArgs 参数并使其正常工作)。您需要处理多个事件才能完成您想要的操作(查看 KeyDownKeyPress 事件)。

或者,使用 MouseClick 事件而不是 Click 事件。

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.

小巷里的女流氓 2024-09-23 08:24:07

好吧,我认为问题在于您对 button1_click() 的声明。

事件处理程序只能具有 So 的签名

delegate void EventHandler(Object sender, EventArgs e)

,将 Key press 从 button1_click 中取出,并将其放入 KeyPress 事件中。

Well, I think that the issue is in your decleration of button1_click().

An event handler can only have the signature of

delegate void EventHandler(Object sender, EventArgs e)

So, take the Key press out of button1_click, and put it in a KeyPress event.

雨夜星沙 2024-09-23 08:24:07

EventHandle委托,是两个参数,而不是三个。
而且你的方法有三个参数,所以它是错误的。

请参阅:msdn 中的 EventHandler 委托信息

首先,你必须修改你的方法:

private void button1_Click(object sender, EventArgs e)
{
}

然后像这样判断方法中的类型:

KeyEventArgs k = null;
if(e is KeyEventArgs){
    k = (KeyEventArgs) e;
    //do sth here about pressing 'enter'
}

委托,方法必须具有相同的参数,相同的返回类型,否则会出现异常。

EventHandle delegate, is two parameters, not three.
and your method has three parameters, so it's wrong.

see:EventHandler delegate information in msdn

first, your have to modify your method:

private void button1_Click(object sender, EventArgs e)
{
}

and then judge the type in the method like this:

KeyEventArgs k = null;
if(e is KeyEventArgs){
    k = (KeyEventArgs) e;
    //do sth here about pressing 'enter'
}

delegate ,method must have the same parameters, the same return type, or it will be exception.

提笔书几行 2024-09-23 08:24:07

我有类似的问题。 EventHandler 委托是一个模板。

public delegate void EventHandler(object sender, TEventArgs e);

因此,如果您更改该行:

this.button1.Click += new System.EventHandler(this.button1_Click);

到:

this.button1.Click += new System.EventHandler(this.button1_Click);

它应该有效。

I had a similar problem. The EventHandler delegate is a template.

public delegate void EventHandler(object sender, TEventArgs e);

So if you change the line:

this.button1.Click += new System.EventHandler(this.button1_Click);

to:

this.button1.Click += new System.EventHandler(this.button1_Click);

it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文