在当前上下文中找不到名称 c# 窗体

huangapple go评论64阅读模式
英文:

name not existing in current context c# form

问题

在最后一行出现错误,说名称不存在的原因是您在代码中尝试使用了一个名为 "label9_Click" 的事件处理程序,但是似乎该事件处理程序未正确定义或声明。要解决这个错误,您需要执行以下步骤:

  1. 确保在您的代码中有一个名为 "label9_Click" 的事件处理程序。您可以在您的代码文件中搜索该事件处理程序,确保它被正确命名和实现。它应该类似于以下示例:
private void label9_Click(object sender, EventArgs e)
{
    // 在这里编写单击事件的处理逻辑
}
  1. 确保事件处理程序的命名与您在代码中使用的名称一致。在您的示例中,您使用了 (label9_Click),这应该与实际事件处理程序的名称匹配。如果事件处理程序的名称不匹配,您需要更改其中一个以使其匹配。

  2. 确保您的事件处理程序位于与标签 label9 相关联的窗体或控件类中。事件处理程序应该在与标签 label9 相同的类中定义,以确保代码能够找到它。

如果您按照上述步骤检查并确认了这些方面,但问题仍然存在,那么可能存在其他代码或配置问题,需要进一步检查。如果您需要更多帮助,请提供更多的代码上下文或详细信息,以便我可以提供更具体的建议。

英文:
label9.AutoSize = true;
label9.BackColor = Color.Transparent;
label9.Font = new Font("Microsoft Yi Baiti", 13.2000008F, FontStyle.Italic, GraphicsUnit.Point);
label9.ForeColor = Color.DimGray;
label9.Location = new Point(28, 354);
label9.Name = "label9";
label9.Size = new Size(134, 23);
label9.TabIndex = 40;
label9.Text = "Phone Number";
label9.TextAlign = ContentAlignment.TopCenter;
label9.Click += (label9_Click); //error here

there is an error on the last line saying the name doesnt exist

can someone please tell me why there is an error there? ive tried creating a controls line but the error was still there also i made this on [design] on vs so im not sure where i went wrong especially because im new to c# and visual studio. am i using the click event wrong?

答案1

得分: 1

  1. 删除错误行
  2. 打开表单设计器
  3. 双击 label9

这些步骤将创建所需的处理程序:

private void label9_Click(object sender, EventArgs e)
{
}
英文:
  1. Delete the error line
  2. Open the Form designer
  3. Double click label9

These steps will create the required handler:

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

答案2

得分: 0

建议是删除错误的行。然后打开表单设计器,双击label9。VS会为您创建适当的事件处理程序。您只需要填写它与您的代码。

英文:

My suggestion is to remove the wrong line. Then open the form designer and double click on label9. VS creates appropriate event handler for you. You need only to fill it with your code.

答案3

得分: 0

从我了解的情况来看,您可能已编辑了表单代码中函数的名称,但未将其设置为Designer.cs中侦听器的新名称。

正如其他人所说,您真的不应该手动编辑Designer.cs文件。这是由表单的设计选项卡自动处理的。

最安全的方法是删除控件,然后再次添加它,然后双击它,它将为您创建必要的代码。

如果您确实想快速修复它,通过编辑Designer.cs文件(我不太建议这样做):

this.label9.Click += new System.EventHandler(this.label9_Click);

然后在表单的主代码窗口中添加此方法:

private void label9_Click(object sender, EventArgs e)
{
    // 执行操作
}
英文:

From what I can tell, you've probably edited the name of the function in your form code, and not set it to the new name for the listener in Designer.cs.

As the others have said, you really shouldn't manually edit the Designer.cs file. That is automatically handled by the design tab of your form.

Safest way would be remove the control and add it back again, then double click it and it will create the necessary code for you.

If you do want to fix it quickly by editing the Designer.cs file (which I wouldn't really recommend):

this.label9.Click += new System.EventHandler(this.label9_Click);

And then in your main code window for the form add this method:

private void label9_Click(object sender, EventArgs e)
{
    // Do stuff
}

huangapple
  • 本文由 发表于 2023年7月27日 23:14:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定