Reputation: 954
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
Upvotes: 8
Views: 3772
Reputation: 99
I know it's an old question but I have been working with custom designers lately and I have a solution for what you want. Add this class to you project:
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace WindowsFormsApplication1
{
[Designer(typeof(BaseFormDesigner), typeof(IRootDesigner))]
public class BaseForm : Form
{
public BaseForm()
{
}
}
public class BaseFormDesigner : DocumentDesigner
{
public override void Initialize(IComponent component)
{
base.Initialize(component);
var service = GetService(typeof(IComponentChangeService)) as IComponentChangeService;
service.ComponentAdded += service_ComponentAdded;
}
private void service_ComponentAdded(object sender, ComponentEventArgs e)
{
if (e.Component is Control && !(e.Component is Form)) {
var control = (Control)e.Component;
if (!control.Name.StartsWith("_")) {
var service = GetService(typeof(IComponentChangeService)) as IComponentChangeService;
PropertyDescriptor desc = TypeDescriptor.GetProperties(control)["Name"];
service.OnComponentChanging(e.Component, desc);
string oldValue = control.Name;
string newValue = "_" + oldValue;
desc.SetValue(control, newValue);
service.OnComponentChanged(e.Component, desc, oldValue, newValue);
}
}
}
}
}
And then change your base form class from Form to BaseForm:
using System;
namespace WindowsFormsApplication1
{
public partial class Form1 : BaseForm
{
public Form1()
{
InitializeComponent();
}
}
}
Close any form designers before recompiling your project and then reopen it and try adding a control, its name will start with underscore.
Upvotes: 5
Reputation: 3637
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.
Upvotes: 3
Reputation: 17648
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.
Upvotes: 0
Reputation: 63173
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.
Upvotes: 11
Reputation: 85996
Resharper can enforce naming conventions. Then it's just a matter of opening the .designer file and hitting alt+enter, enter a few times.
Upvotes: 0
Reputation: 4259
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.
Upvotes: 0
Reputation: 66573
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()
.
Upvotes: 1