Winforms - 直观地删除按钮单击事件

发布于 2024-08-30 20:51:01 字数 562 浏览 7 评论 0原文

.NET 新手警报

使用 Visual C# 2008 Express Edition 我不小心为按钮创建了单击事件。然后,我删除了自动创建的方法代码,这导致出现错误,指出无法再找到现在在表单加载代码中引用的函数。

Form1.Designer.cs 文件的 InitializeComponent() 函数中删除以下行...

this.btnCopy.Click += new System.EventHandler(this.btnCopy_Click);

...似乎可以解决问题,但是,这让我感觉很脏由于 #region 开头出现以下警告:

/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.

我无法找到使用表单设计器执行此操作的方法,我认为这是此警告所暗示的方法。这样做的正确方法是什么?

.NET newbie alert

Using Visual C# 2008 Express Edition I have accidentally created a click event for a button. I then deleted the automatically-created method code, which resulted in an error saying that the function, which had now been referenced in the form loading code, could no longer be found.

Deleting the following line from the Form1.Designer.cs file's InitializeComponent() function...

this.btnCopy.Click += new System.EventHandler(this.btnCopy_Click);

... seems to do the trick, however, it makes me feel very dirty because of the following warning at the beginning of the #region:

/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.

I haven't been able to find a way to do this using the form designer, which I assume is the means implied by this warning. What is the correct way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我只土不豪 2024-09-06 20:51:01

在使用 Designer.cs 文件时,您确实必须小心,但您不必对此感到不舒服(当我犯同样的错误时,在 Designer.cs 文件中修复它会更容易)。您可以像这样直观地完成此操作:

  1. 在表单设计器中打开表单。
  2. 在表单设计器中,单击感兴趣的按钮。
  3. 按 F4(或右键单击该按钮,然后单击属性)。应该会显示属性窗格。
  4. 在属性窗格的顶部,单击闪电。这显示了按钮的事件。
  5. 找到点击事件并清除其处理程序。

You do have to be careful when working in the designer.cs files but you don't have to feel dirty about it (when I make the same mistake it is just easier to fix it the designer.cs file). You can do it visually like this:

  1. Open the form in the form designer.
  2. In the form designer, click the button of interest.
  3. Press F4 (or right click the button and then click properties). The properties pane should show up.
  4. At the top of the properties pane, click the lightning bolt. This shows the events for the button.
  5. Find the click event and clear its handler.
盗梦空间 2024-09-06 20:51:01

好吧,我通常是提倡使用 notepad2 或其他文本编辑器来执行编码任务的人。

但是,既然您询问如何在设计器中执行此操作...

  1. 打开将错误事件添加到控件的表单。
  2. 选择控件。
  3. 右键单击,选择“属性”。
  4. 通过选择带有闪电图标的按钮更改为“事件”。
  5. 选择您需要删除的事件。
  6. 将鼠标放在显示事件处理程序方法名称的框中后,删除该框中的所有文本,然后按 Enter 键。这将删除控件上该事件的事件处理程序和委托分配。

唯一需要注意的是:如果您希望保留事件处理程序方法(即它不是由 Visual Studio 自动生成),您可能希望避免以这种方式删除分配。因为当我说它删除事件处理程序时 - 我应该说“Form1.cs”(例如)中事件处理程序方法的声明也将被删除。

Okay, I am usually the one advocating the use of notepad2 or some other text editor to perform coding tasks.

But, since you ask how to do so in the Designer...

  1. Open the form where the erroneous event was added to a control.
  2. Select the control.
  3. Right-click, select "Properties".
  4. Change to "Events" by selecting the button with the lighting-bolt icon.
  5. Select the event you need to remove.
  6. After placing the mouse in the box which is showing the event handler method name, delete all of the text in that box and press enter. This will remove the event handler and the delegate assignment for this event on your control.

The only caveat being: if you wish to preserve your event handler method (i.e. it is not auto-generated by Visual Studio) - you probably want to avoid deleting the assignment in this manner. Because when I say that it removes the event handler - I should say that the declaration of the event handler method in "Form1.cs" (for example) will be deleted as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文