问题描述
我正在开发一个项目,我直到最近才把所有的控件留给了默认名称。在项目开始时多次,我不小心双击了一些东西,并为该控件创建了一个事件处理程序。当时我不知道如何删除它们,因为Ctrl + Z将远远超出我想要的。
I am working on a project that I had left all the controls with default names until recently. Multiple times at the start of the project, I accidentally double-clicked something and it created an event handler for that control. At the time I didn't know how to remove them, because Ctrl+Z would undo far more than I wanted.
我现在知道我可以查看和重构属性
窗口的事件
部分中的控件使用的事件。我主要修复了这些问题,但是我有一个名为 TabPage4_Click
的功能,我找不到实际使用它的控件。我没有TabPage4了,我已经重命名了所有这一切,而且我的标签页都没有使用这个事件处理程序,但如果我删除它,我的应用程序无法编译。
I now know that I can view and refactor the events that are being used by a control in the Events
section of the Properties
windows. I have mostly fixed these problems but I have a function called TabPage4_Click
that I can't find a control that is actually using it. I don't have a TabPage4 anymore, I have renamed everything at this point, and none of my tab pages are using this event handler, but my application fails to compile if I remove it.
在这一点上,我的表单上有太多的控件来逐一检查它们,以确定他们是否使用这个事件处理程序。我想知道是否有任何方法可以查看事件处理程序列表和使用它们的控件。在Visual Studio 2010中有什么办法可以方便地跟踪目前正在使用的事件处理程序,以便我删除它而不产生任何冲突?
At this point, there are far too many controls on my form to check them one-by-one to find out if any of them is using this event handler. I am wondering if there is any way for me to view a list of event handlers and the controls that are using them. Is there any way in Visual Studio 2010 to easily track down what control is currently using an event handler so that I remove it without creating any conflicts?
推荐答案
避免使用设计器,除了在表单上添加控件。如果您在表单初始化时明确注册其事件,则会发现事件和事件处理程序更容易管理:
Avoid using the designer for anything other than adding controls onto your form. You'll find events and event handlers much easier to manage if you're explicitly registering their events upon form initialization:
void RegisterControlEvents()
{
myButton.Click += MyClickHandler;
...
}
void MyClickHandler(object sender, EventArgs e)
{
...
}
很快你会发现自己没有使用设计师...
Soon you'll find yourself not using the designer at all...
对于死代码,删除它并尝试构建;如果有一个参考,VS会告诉你删除哪里删除你的代码/去哪里去修复它。
As for dead code, delete it and try building; if there's a reference, VS will tell you where the deletion broke your code / where to go to fix it.
如果你正在寻找一个工具,我会说得到ReSharper演示。我不认为有什么比较(不,我不适用于JetBrains / ReSharper,这只是一个了不起的工具)。
If you're looking for a tool, I'd say get the ReSharper demo. I don't think anything else compares (no I don't work for JetBrains/ReSharper, it's just an amazing tool for that).
这篇关于删除Visual Studio 2010事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!