Winforms - 直观地删除按钮单击事件
.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在使用 Designer.cs 文件时,您确实必须小心,但您不必对此感到不舒服(当我犯同样的错误时,在 Designer.cs 文件中修复它会更容易)。您可以像这样直观地完成此操作:
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:
好吧,我通常是提倡使用 notepad2 或其他文本编辑器来执行编码任务的人。
但是,既然您询问如何在设计器中执行此操作...
唯一需要注意的是:如果您希望保留事件处理程序方法(即它不是由 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...
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.