“insert_method_here”没有重载匹配委托“System.EventHandler”
我正在尝试构建一个具有按钮的程序,每次单击该按钮时,它都会移动按钮并添加到分数中。但是,我试图禁用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的方法签名与
EventHandler
委托不匹配(也就是说,您不能仅添加KeyEventArgs
参数并使其正常工作)。您需要处理多个事件才能完成您想要的操作(查看KeyDown
或KeyPress
事件)。或者,使用
MouseClick
事件而不是Click
事件。Your method signature does not match
EventHandler
delegate (that is, you cannot just addKeyEventArgs
argument and get this working). You'll need to handle more than one event to do what you want (look atKeyDown
orKeyPress
events).Alternatively, use
MouseClick
event instead ofClick
event.好吧,我认为问题在于您对
button1_click()
的声明。事件处理程序只能具有 So 的签名
,将 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
So, take the Key press out of
button1_click
, and put it in aKeyPress
event.EventHandle委托,是两个参数,而不是三个。
而且你的方法有三个参数,所以它是错误的。
请参阅:msdn 中的 EventHandler 委托信息
首先,你必须修改你的方法:
然后像这样判断方法中的类型:
委托,方法必须具有相同的参数,相同的返回类型,否则会出现异常。
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:
and then judge the type in the method like this:
delegate ,method must have the same parameters, the same return type, or it will be exception.
我有类似的问题。 EventHandler 委托是一个模板。
public delegate void EventHandler(object sender, TEventArgs e);
因此,如果您更改该行:
到:
它应该有效。
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:
to:
it should work.