[英]Can not build, get error: No overload for'Form1_Load' matches delegate 'Event Handler
i am trying to make a program that when i press F, it waits a few seconds then presses Enter. 我正在尝试制作一个程序,当我按F键时,它会等待几秒钟,然后按Enter键。 But i get an error saying
但我说错了
No overload for'Form1_Load' matches delegate 'Event Handler
“ Form1_Load”没有重载匹配委托“事件处理程序”
Here is my Form1.Designer.cs: 这是我的Form1.Designer.cs:
namespace csgossprogram
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(102, 114);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 264);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
}
}
And here is my code for Form1.cs 这是我的Form1.cs代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace csgossprogram
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F)
{
SendKeys.Send("~");
}
}
}
}
Using Form.Load
event in this manner has no any point. 以这种方式使用
Form.Load
事件没有任何意义。 It is invoked when form loads and there is no key to check if it is F. That's also why Form.Load
expects handler with Form1_Load(object sender, EventArgs e)
signature. 加载表单时调用它,并且没有任何键检查它是否为F。这也是为什么
Form.Load
期望具有Form1_Load(object sender, EventArgs e)
签名的处理程序的原因。
To do what you wanted you should use Form.KeyDown
or Form.KeyUp
event: 1. Rename Form1_Load
to Form1_KeyDown
or Form1_KeyUp
, depending what you have chosen to use. 要执行所需的操作,应使用
Form.KeyDown
或Form.KeyUp
事件:1.根据您选择的使用方式,将Form1_Load
重命名为Form1_KeyDown
或Form1_KeyUp
。 This is optional but it's better to do it so you keep naming consistent. 这是可选的,但最好这样做,以使命名保持一致。 2. Instead:
this.Load += new System.EventHandler(this.Form1_Load);
2.相反:
this.Load += new System.EventHandler(this.Form1_Load);
use either this.Load += this.Form1_KeyDown;
使用
this.Load += this.Form1_KeyDown;
or this.Load += this.Form1_KeyUp;
或
this.Load += this.Form1_KeyUp;
depending on event you have chosen. 取决于您选择的事件。
Note, you don't have to use constructor for delegate. 注意,您不必为委托使用构造函数。 Just write method name after
+=
(you can omit this
, too). 只需在
+=
后写方法名称(您也可以省略this
名称)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.