Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you change VS Win Forms designer code generation?

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

like image 922
Big Endian Avatar asked Apr 08 '10 13:04

Big Endian


4 Answers

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.

like image 54
Lex Li Avatar answered Oct 27 '22 20:10

Lex Li


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.

like image 29
Bryan Batchelder Avatar answered Oct 27 '22 20:10

Bryan Batchelder


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.

like image 24
darko79 Avatar answered Oct 27 '22 22:10

darko79


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().

like image 38
Timwi Avatar answered Oct 27 '22 22:10

Timwi