Search code examples
c#compiler-errorsauto-generate

disable automatic changes in designer.cs


I have created a new button class within my project:

using System;
using System.Windows.Forms;

namespace CEditor
{
    public class CustomButton : Button
    {
        public CustomButton()
        {
            this.SetStyle(ControlStyles.Selectable, false);
        }
    } 
}

After I drop the custom button on the form, two lines of code are generated in the designer.cs, each producing the same error:

namespace CEditor
{
    partial class CEditor
    {
        private CEditor.CustomButton button1;
        // Error:
        // The type name 'CustomButton' does not exist in the type 'CEditor.CEditor'

        this.button1 = new CEditor.CustomButton();
        // Error:
        // The type name 'CustomButton' does not exist in the type 'CEditor.CEditor'
    }
}

Everything runs fine, if I remove the "CEditor." parts from both lines, so far so good, but as soon as I generate an event per double-click in the property panel of a control, the designer "repairs" the above lines and I have to delete the "CEditor." parts again, which gets more annoying the more buttons there are.

Is there anything I can do to stop that behavior?


Solution

  • This is why you shouldn't give your class the same name as its namespace.

    Change either name (eg, CEditorControl) and you'll be fine.