C#-构造函数中的加载方法
有人能解释一下为什么(在构造函数中)执行此方法会引发语法错误:C#-构造函数中的加载方法,c#,.net,windows,constructor,C#,.net,Windows,Constructor,有人能解释一下为什么(在构造函数中)执行此方法会引发语法错误: public partial class Form1 : Form { public Form1() { Load += YourPreparationHandler; } private void YourPreparationHandler(object sender, EventArgs e) { button22_Click(sender, e);
public partial class Form1 : Form
{
public Form1()
{
Load += YourPreparationHandler;
}
private void YourPreparationHandler(object sender, EventArgs e)
{
button22_Click(sender, e);
}
}
名称“button22\u Click”不适用
存在于当前上下文中
按钮22\u点击是否为表单1的成员?检查方法是否存在,错误是不言自明的。是否在任何地方定义了按钮22\u单击?如果不是的话,这就是问题所在。通常在构造函数中有如下内容:
public Form1()
{
InitializeComponent();
}
表单类设置为分部类。这是因为在VisualStudio中,当您将组件拖放到窗体上时,幕后VS正在使用您的更新更新设计器文件
所以,你会有
表格1.cs
Form1.Designer.cs
还有可能
Form1.xx.resx(如果您有全球化)
如果查看设计器文件,您将看到Visual Studio正在生成的类似代码:
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.buttonTest = new System.Windows.Forms.Button();
this.textBoxPW = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.textBoxOutput = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// buttonTest
//
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
///
私有void InitializeComponent()
{
this.buttonest=new System.Windows.Forms.Button();
this.textBoxPW=new System.Windows.Forms.TextBox();
this.label1=new System.Windows.Forms.Label();
this.textBoxOutput=new System.Windows.Forms.TextBox();
this.label2=new System.Windows.Forms.Label();
这个.SuspendLayout();
//
//钮扣测试
我打赌设计器文件丢失、混乱或InitializeComponent被意外删除。在任何情况下,对象(按钮22)不存在或未被引用,因此您将无法在其上引发单击事件。Uhm,我将发表评论,即使答案很明显,因为我相信问题不是您所说的,或者代码不是您所显示的。当前上下文中不存在方法按钮22\u click
(也就是说,从你调用它的地方就可以到达。)你验证过该方法确实存在吗?在问题的代码中,它肯定不存在。请原谅,但是“button22_Click”是否存在是否存在于课堂中?杰兹,谁对这里似乎正确的答案投了反对票?这里的代码大部分是对上一个问题答案的复制+粘贴。不起作用的部分是迈克添加的部分。请看:没有理由对这个答案投反对票。投反对票是为了补偿。至少,它应该在马修的答案上获得同样的分数-它们是同义词。是的,但马修是第一个。或者说,拉西是第一个,在他的评论中。没有理由对这个答案投反对票。投反对票是为了补偿。我不明白为什么有人会想投反对票。这个星球上有一些奇怪的人。。。。