在 C# 中,如何轻松更改事件处理程序的名称?
在VS2008中,如果我双击事件处理程序,VS会创建一个具有默认名称的默认事件处理程序,例如combobox1_SelectedIndexChanged。
举例来说,我现在将combobox1 重命名为cbStatus。它仍然具有相同的事件处理程序,因此我现在将其更改为 cbStatus_SelectedIndexChanged。
有没有办法,VS 可以将初始的combobox1_SelectedIndexChange 更改为cbStatus_SelectedIndexChange,而不是除了旧的事件处理程序之外还生成新的cbStatus 事件处理程序?因为每次我都必须将代码剪切并粘贴到新的事件处理程序中,然后删除旧的。
此外,如果我已经定义了初始事件处理程序,然后不再需要该处理程序,则我不能简单地从代码中删除该处理程序,因为表单设计者会抱怨它无法找到原始事件处理程序。有没有办法让 VS 可以自动从表单设计器中删除事件处理程序的分配?
我似乎花了一整天的时间来剪切和粘贴,并从表单设计器代码中删除事件处理程序分配。
In VS2008, if I double click on the event handler VS creates a default event handler with a default name, e.g. combobox1_SelectedIndexChanged.
Say, for example, i now rename combobox1 to cbStatus. It still has the same event handler, so i now change that to cbStatus_SelectedIndexChanged.
Is there a way, where VS can change the initial combobox1_SelectedIndexChange to cbStatus_SelectedIndexChange rather than generate a new cbStatus event handler in addition to the old event handler? Because every time i have to cut and paste the code to the new event handler and then delete the old one.
In addition, if i have defined the initial event handler and then no longer require the handler, i cannot simply delete the handler from code, as the form designer then complains that it cant find the original event handler. Is there a way where VS can automatically remove the assignment of the event handler from the form designer?
I seem to be spending all day cutting and pasting, and deleting event handler assignments from the forms designer code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您重命名控件时,您也应该重命名事件处理程序。正确的方法是重构代码。
为此,只需在 Visual Studio 代码编辑器中右键单击事件处理程序的名称,然后选择“重构”->“重构”。重命名... 这将允许您在使用它的任何地方自动更改它的名称。
对于事件处理程序,它可能只在另一个地方使用(将其添加到事件的代码中的点),因此手动更改它并不会太麻烦。不过,您可以将此技术应用于几乎所有内容,当您正在更改的内容被从多个不同的地方引用时,它非常有用。
When you rename the control, you should rename the event handler too. The proper way to do this is by refactoring the code.
To do this, just right-click the name of the event handler in the Visual Studio code editor and choose
Refactor -> Rename...
That will allow you to automatically change the name of it everywhere it's used.In the case of an event handler, it's probably only used in one other place (the point in code where it's added to the event), so it's not too much trouble to change it manually. You can apply this technique to pretty much anything, though, making it extremely useful when something you're changing is referred to from several different places.
您只需在生成的代码中找到声明
combobox1_SelectedIndexChange
方法的位置,并将名称更改为cbStatus_SelectedIndexChange
即可。更改方法名称后,您还必须更新注册处理程序的行:
You just have to find the place in the generated code where the
combobox1_SelectedIndexChange
method is declare and change the name tocbStatus_SelectedIndexChange
.After you change the method name, you also have to update the line where you register the handler:
只需键入新名称,然后重新编译即可。我的意思是 - 更改
为
然后重新编译
Visual Studio 将引发编译时错误,因为预期的方法不再存在。
双击“输出”窗口中的错误以转到错误处理程序的分配,并在那里更改错误处理程序以匹配新的函数名称。
编辑 - 添加
上面的步骤将跳转到贾斯汀的答案中描述的代码行...
结束编辑
我知道这很清楚,但是尝试一下,你就可以了。会很容易或没有困难地解决它。
Just type the new name, then recompile. By this I mean - Change
to
and then recompile
Visual Studio will throw a compile-time error, because the method that is expected is no longer there.
Double-click on the error in the Output window to go to the assignment of the error handler, and change the error handler there to match the new function name.
Edit - added
The above step will jump you to the line of code described in Justin's answer...
End Edit
I know that's clear as mud, but try it and you'll figure it out with little or no difficulty.
如果您单击而不是双击来自动创建事件处理程序,则可以指定所需的处理程序名称。您可以将其设置为类似于“SelectedStatusChangedHandler”,它独立于组合框的变量名称。然后按“enter”键,让 VS 为您创建处理程序。
If you single-click instead of double-clicking to automatically create the event handler, you can specify the handler name you want. You could make it something like "SelectedStatusChangedHandler", which is independent of the combobox's variable name. Then press 'enter' and let VS create the handler for you.