1

I have created a custom user control like this (simple example):

public class MyButton : Button { public MyButton() { BackColor = Color.Blue; } } 

After compiling, it shows up in the form designer toolbox, and is displayed with the updated property. But, if I change the color, the controls that I am already using on the form keep their original color.

Is there a way to design a control that will propogate its changes to the designer?

3
  • When you say you change the colour, do you mean you change the line of code "BackColour = Color.Blue" or do you change the property of 1 instance of the control via the VS Properties page? Commented Jan 29, 2010 at 15:06
  • When happens if you then run the whole application (F5) do all the controls update/change then? Commented Jan 29, 2010 at 15:16
  • Negative. Basically the controls stay in the state they were in when created/dragged on to the form. Commented Jan 29, 2010 at 15:34

3 Answers 3

5

The problem is that buttons you dropped on the form before you edited the class are already being initialized by the form's InitializeComponent() call with a different color. In other words, they override the default color you set in the constructor. What you have to do is declare the default value of the property so that the designer doesn't generate an assignment. That should look like this:

using System; using System.Drawing; using System.ComponentModel; using System.Windows.Forms; class MyButton : Button { public MyButton() { BackColor = Color.Blue; } [DefaultValue(typeof(Color), "Blue")] public override Color BackColor { get { return base.BackColor; } set { base.BackColor = value; } } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Technically this works, for a single control on the form. When I drop a second control on to the form and then change the color, then the original color persists. Any ideas?
Look in the Designer.cs file and make sure the BackColor property isn't being assigned. Just delete the assignments if these buttons were added before you changed the code.
1

I don't know if that is possible, but I do know that when you drag a control on the designer, its constructor is executed. That's why your button will be blue at first, and will not change color afterwards.

Comments

0

You are setting default value in the constructor for BackColor property,when you launch the designer it actually sets default values from constructor.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.