0

I need to redesign old windows forms application. So I decided to create a custom button control.

Let's say I defined a background color to be red for that custom button. When I do this:

  • place that button on the form
  • edit property of any other control
  • save the project

Visual Studio will automatically update the designer file and append:

this.btnCustom.BackColor = System.Drawing.Color.Red;

I don't want this to happen. This should be coded only in CustomButton class, not in the designer file. This will be the big problem if I decide to change button color to blue. Then, instead changing color only in CustomButton class, I would have to change color manually for each button. Is there any way to prevent this?

zoran
  • 943
  • 11
  • 22
  • See this https://stackoverflow.com/questions/9731363/how-to-set-design-time-property-default-values – vasek Jul 19 '17 at 10:35
  • @vaske Thank you, but I still can't make it work. I tried everything from that link... – zoran Jul 19 '17 at 12:40

1 Answers1

1

If your CustomControl code is something like this:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override Color BackColor
    {
        get
        {
            return Color.Red;
        }
    }
}

Then, when placed on Form, designer doesn't generate setter for BackgroundColor:

        // 
        // userControl11
        // 
        this.userControl11.Location = new System.Drawing.Point(67, 131);
        this.userControl11.Name = "userControl11";
        this.userControl11.Size = new System.Drawing.Size(154, 37);
        this.userControl11.TabIndex = 2;

So if you change your CustomControl later, it gets changed everywhere used.

vasek
  • 2,759
  • 1
  • 26
  • 30
  • How can I use this on the fields that cannot be overridden? For example, on AutoSizeMode – zoran Jul 19 '17 at 12:53
  • https://stackoverflow.com/questions/1528371/hiding-unwanted-properties-in-custom-controls – vasek Jul 19 '17 at 12:59