Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Customising c# code generation

Status
Not open for further replies.

heyrudi

Programmer
Feb 19, 2004
1
GB
Hello all,

Microsoft Visual Studio does a lot of work for us when creating windows forms. However, it doesn't know anything about individual conventions for naming variables etc.

Hence, when I drag an TextBox control onto a form I get a private member called TextBox1. Since all my private members always start with "m_" I would like to change this behaviour if possible.

The above example is a trivial manner in which I'd like to customise code generation in Visual Studio - More important ones are things like the default names of event handlers when you double click the handler in the properties page or double click the control. All my methods start with a capital letter - if my TextBox is called m_NameTextBox, then the default event handler created will be m_NameTextBox_TextChanged - I always have to remove the "m_" to keep the method within my naming conventions.

If anyone knows how to trap these events and customise them with an Add-In or where the rules for the names are defined (maybe in a template file somewhere), please let me know.

Many thanks,
Darryl.
 
There is no problem to name the objects like you want.
The solution is the following:
-Drag from ToolBox the type of object you want on your form, let say a TextBox.
- After you see it on the form then cklick on it with right mouse button and select Properties
- Scroll the properties and you will see (name) textBox1. Click on it and change it to m_TextBox1
- Click on the Events in the same Properties window and scrool to locate , let say, TextChanged event. Double click on it and you will see the generated prototype to :
private void m_TextBox1_TextChanged(object sender, System.EventArgs e)
{

}
Repeat the above steps for all other objects on the form when you name it during the designer.
-obislavu-
 
I don't know if anything can be done during the process of adding a control to a form... each control's code is responsible for naming the instance of the control. So, when you drop a Windows forms textbox on a form, the textbox class (that's in the framework) is responsible for generating the name.

You could do it by creating your own suite of controls -- that way you'd have control over the name that gets generated.

Or, you could write an add-in to change the name after the fact, using the CodeDOM.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top