在C#中,如何轻松更改事件处理程序的名称? [英] In C#, how can you easily change the name of an event handler?

查看:163
本文介绍了在C#中,如何轻松更改事件处理程序的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VS2008中,如果我双击事件处理程序VS,则创建一个具有默认名称的默认事件处理程序,例如: combobox1_SelectedIndexChanged。

In VS2008, if I double click on the event handler VS creates a default event handler with a default name, e.g. combobox1_SelectedIndexChanged.

比如说,我现在将combobox1重命名为cbStatus。它仍然具有相同的事件处理程序,所以我现在将其更改为cbStatus_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.

有没有办法,VS可以将初始的combobox1_SelectedIndexChange更改为cbStatus_SelectedIndexChange而不是生成新的cbStatus事件处理程序以及旧的事件处理程序?因为每次我必须将代码剪切并粘贴到新的事件处理程序然后删除旧的。

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.

此外,如果我已经定义了初始事件处理程序并且然后不再需要处理程序,我不能简单地从代码中删除处理程序,因为表单设计者然后抱怨它无法找到原始事件处理程序。是否有一种方法可以让VS自动从表单设计器中删除事件处理程序的赋值?

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.

推荐答案

重命名控件时,还应重命名事件处理程序。正确的方法是通过重构代码。

When you rename the control, you should rename the event handler too. The proper way to do this is by refactoring the code.

要做到这一点,只需右键单击事件处理程序的名称即可。 Visual Studio代码编辑器并选择 Refactor - >重命名... 这将允许您自动更改所使用的的名称。

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.

这篇关于在C#中,如何轻松更改事件处理程序的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆