简体   繁体   English

C# 如何正确删除事件处理程序代码?

[英]C# How to properly remove event handler code?

I have a keyup event that I want to remove from my C# Winform:我有一个要从 C# Winform 中删除的keyup事件:

private void txtInputBox_KeyDown(object sender, KeyEventArgs e)
{
      if (e.KeyCode == Keys.Return)
      {
          cmdSend.PerformClick();
      }
  }

If I comment out or delete this function I get an error in the design view of the form:如果我注释掉或删除这个函数,我会在表单的设计视图中得到一个错误:

"The designer cannot process unknown name 'txtInputBox_KeyDown' at line 66. The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified. Please remove any changes and try opening the designer again." “设计器无法处理第 66 行的未知名称 'txtInputBox_KeyDown'。方法 'InitializeComponent' 中的代码由设计器生成,不应手动修改。请删除任何更改,然后再次尝试打开设计器。”

The program also will not compile.该程序也不会编译。

It happens if I try to delete any event handler code on my form.如果我尝试删除表单上的任何事件处理程序代码,就会发生这种情况。 I am teaching myself C# from my background in VB.NET and in VB.NET I could remove event handler code without issue.我正在从我在 VB.NET 和 VB.NET 的背景中自学 C#,我可以毫无问题地删除事件处理程序代码。

It is because in Designer.cs event handler is registered and when you just delete from code behind it's method, it does not remove it's event registration from Designer.cs这是因为在Designer.cs中注册了事件处理程序,当您只是从其方法后面的代码中删除时,它不会从Designer.cs 中删除它的事件注册

you should remove event from form designer view by going in control properties so that this issue not comes or from Designer.cs remove event registration line which will be like:您应该通过进入控件属性从表单设计器视图中删除事件,以便此问题不会出现或来自Designer.cs删除事件注册行,如下所示:

SomeTextBox.KeyUp += SomeTextBox_KeyUp

thats because in C#, the functions are added to the events programatically by the designer, when you add it in the designer.那是因为在 C# 中,当您将其添加到设计器中时,设计器以编程方式将函数添加到事件中。

In the solution explorer window, Expand Form1.cs and open the Form1.Designer.cs file there:在解决方案资源管理器窗口中,展开Form1.cs并在Form1.Designer.cs打开Form1.Designer.cs文件:

在此处输入图片说明

then locate the function :然后找到函数:

private void InitializeComponent()

and delete the line that registers your event handler to the event.并删除将事件处理程序注册到事件的行。 it will have a += operator in the middle like this:它将在中间有一个+=运算符,如下所示:

在此处输入图片说明

your event will be located at line 66 and will look like this:您的事件将位于第 66 行,如下所示:

this.txtInputBox.KeyDown += new System.EventHandler(this.txtInputBox_KeyDown);

In the designer code that is automatically generated by Visual Studio the event is still linked to this non-existent function.在 Visual Studio 自动生成的设计器代码中,事件仍然链接到这个不存在的函数。

In the Solution Explorer, find your Form.Designer.cs file (it is in a subnode under the main Form.cs), or use the Find in Solution function to find the line that says在解决方案资源管理器中,找到您的 Form.Designer.cs 文件(它位于主 Form.cs 下的子节点中),或使用“在解决方案中查找”功能找到显示

txtInputBox.KeyDown += new System.EventHandler(txtInputBox_KeyDown);

Delete the line, and it should work again.删除该行,它应该会再次工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM