armnotstrong
armnotstrong

Reputation: 9065

Change name of winform components of C# in code with visual studio

When I create a win-form project and add some components to the form by drag them from the tool box, everything works nice, binding codes are generated but components' names are given by default, like button1, button2... I think they are not appropriate for code maintenance.

So I want to name them like button_add, button_del and some name like that, more meaningful, so I change the name in the attribute box of the component, but I don't see any change in the code.

Can some one show me how to do this?

enter image description here

Upvotes: 1

Views: 4641

Answers (1)

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

Usually such changes should be performed automatically by Visual Studio when you change the control's name. The only thing not being renamed are event handlers.

For example: If you add a Click handler to a button named button1 the method will be named button1_Click by Visual Studio. The method name will not change if you rename the button to button_add. This is probably because you can name the method yourself, for example ItWasPressed - how should Visual Studio handle this? If it renamed the method, you'd complain that Visual Studio turned your nicely named method into button_add_Click automatically :-)

You can simplify this refactoring-step by modifying the method name, then pressing Shift+Alt+F10 and selecting the respective option to rename all occurrances.

However, all other references in code will be changed, like for example button1.Enabled = false; will be changed to button_add.Enabled = false;.

Upvotes: 2

Related Questions