Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, how can you easily change the name of an event handler?

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.

like image 901
Simon Avatar asked Dec 12 '22 20:12

Simon


1 Answers

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.

like image 66
Rich Avatar answered Mar 06 '23 07:03

Rich