views:

346

answers:

6

We implemented new coding standards, which call for our private members to have a leading underscore. Like so:

private System.Windows.Forms.Label _label;

Unfortunately VS will put out the default below when you drag a new label onto your form:

private System.Windows.Forms.Label label1;

Is there a way to change the default to:

private System.Windows.Forms.Label _label1;

This pertains to all controls, not just labels, and yes they are to be used from code.

Cheers,

Plamen

+1  A: 

This answer may be unsatisfactory, but it's the best I can offer.

I don't think you can get the Visual Studio Designer to automatically add the underscore. However, what you can do is make the process of adding the underscore less painful. Just create your objects without the underscore first; then, use the Refactor feature to rename them. Just place your cursor on the name of the field (label1, in your case) and press F2 (or right-click ⇒ Refactor ⇒ Rename). This works no matter where in the code you are, all you need is a mention of label1. Since you'll probably be writing code that uses your controls, you'll probably be referencing the fields anyway. You can also press F12 to get straight to the declarations where you effectively get a full list of all your controls; you can easily use F2 to rename lots of them there in one go.

Incidentally, I also use F2 to rename most of the auto-generated event handler names. For example, I hate to see method names like btnZoomIn_Click(). I prefer to call the method zoomIn() because that is what it does. Instead of Mainform_KeyPress() I might call it processKeyPress().

Timwi
Thanks for the answer, Timwi, I am aware of the refactoring capabilities of VS. Changing the name at design-time is a lot faster then doing it from code, I suspect it is optimized. Doing it from code compiles all your assemblies even for privates for whatever reason. I am looking for something I can set and forget, and would not require constant maintenance/human work.
Big Endian
A: 

You can set the GenerateMember to false.
This doesn't resolve your problem, but avoid for wrong naming, when you dont to work with control manually.

Avram
A: 

Resharper can enforce naming conventions. Then it's just a matter of opening the .designer file and hitting alt+enter, enter a few times.

BlueRaja - Danny Pflughoeft
+9  A: 

Personally I believe automatic generated code should be excluded to any coding guidelines and so on. It is safe to ignore them in most scenarios, unless the generator has a bug.

Please debate with whoever wrote that coding guidelines and ask him.her to exclude generated code.

Lex Li
I agree. If you try taming auto-generated code, you'll never get anything productive done. It may be "just" the WinForms Designer code today, but tomorrow you might have to deal with WCF's clients, Entity Framework mapping and maybe even monsters like what ANTLR generates. Lex is right, auto-generated code must be excluded from these kinds of requirements.
Allon Guralnek
A: 

As BlueRaja said, you could use Resharper. You could then use its Code Cleanup (Ctrl-E, C), however this doesn't work on generated files, so you'll have to temporarily rename it. I'm not at work, so I can't check if there is an option to enable the option for generated files somewhere.

Note that you'll have to rerun this every time the file is regenerated.

Daniel Rose
+1  A: 

Beyond creating your own VS Add-In that watches for generated code and re-names it automatically, there is no way to accomplish what you want.

Bryan Batchelder