views:

89

answers:

4

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.

A: 

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.

Anna Lear
A: 

Just type the new name, then recompile. By this I mean - Change

protected void combobox1_SelectedIndexChanged(object sender, EventArgs e)
{

}

to

protected void renamedcombobox_SelectedIndexChanged(object sender, EventArgs e)
{

}

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.

David Stratton
+1  A: 

You just have to find the place in the generated code where the combobox1_SelectedIndexChange method is declare and change the name to cbStatus_SelectedIndexChange.

After you change the method name, you also have to update the line where you register the handler:

cbStatus.SelectedIndexChange += new
    SelectedIndexChangeEventHandler(cbStatus_SelectedIndexChange);
Justin Niessner
+5  A: 

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.

Rich
duh. I never thought of that. Boy do I feel dumb now. Here I've been breaking my code, compiling it, and then fixing it. Thanks, and +1.
David Stratton