Windows Develop Bookmark and Share   
 index > Windows Forms General > Problem when overriding controls backcolor property
 

Problem when overriding controls backcolor property

Hi, i override backcolor property of a control.
Then add this control to a form
In property window changed back color property to red while its default was black
I see the change in design environment
But when i run application i see the control back color is black
[Browsable(true)]
[Category("Colors")]
public override Color BackColor
{
      get { return backColor; }
      set
      {
             if (backColor == value)
                 return;
             backColor = value;
             Invalidate();
       }
}
Not that i also override onpainting event to fill custom region with custom backcolor
Koray Samsun  Monday, October 05, 2009 5:05 PM
Did you compile the project and then set the property again after changing the code.

Since you set the base's backcolor it should serialize.

I reproduced error on my machine and solved it by this way.
  • Marked As Answer byKoray Samsun Monday, October 05, 2009 6:10 PM
  •  
Tamer Oz  Monday, October 05, 2009 6:00 PM
Hard to tell without seeing onpainting event.

You use backColor variable instead of base classes BackColor property(base.BackColor);

if you are calling base.OnPaint after your code it may be overriding the code you wrote.
Tamer Oz  Monday, October 05, 2009 5:18 PM
i'm confused. I see that when i change backcolor prop on property window, it does not take place in Form1.cs[Design] as expected. It has to be something like myControl.BackColor = Color.Red but seems nothing
Koray Samsun  Monday, October 05, 2009 5:18 PM

Hmm, may be you're right. paint code is below

 

protected override void OnPaint(PaintEventArgs e)
        {
            OnDrawBackground(e.Graphics, backColor);
            base.OnPaint(e);
        }

        protected virtual void OnDrawBackground(Graphics g, Color backgroundColor)
        {
            GraphicsPath path = null;
           
                Rectangle rec = new Rectangle(ClientRectangle.X, ClientRectangle.Y,
                    ClientRectangle.Width - 1, ClientRectangle.Height - 1);
                path = GraphicsHelper.GetRoundedPath(rec, radius);
                       
          
                //rounded control, so put parent background to all control to fill corners
                // and backcolor to rounded region
                if (Parent != null)
                {
                    using (Brush brPar = new SolidBrush(Parent.BackColor))
                    {
                        g.FillRectangle(brPar, ClientRectangle);
                    }
                }

                g.SmoothingMode = SmoothingMode.AntiAlias;
                                   using (Brush br = new SolidBrush(backgroundColor))
                    {
                        g.FillPath(br, path);
                    }
                
            
                         path.Dispose();

            
        }
Koray Samsun  Monday, October 05, 2009 5:25 PM
Yes that's the problem I think,

Use base.BackColor instead of backColor, or use them together, and inform us about the situation.

Tamer Oz  Monday, October 05, 2009 5:35 PM
The property value is not being serialized into the designer.  In my limited experience, this is caused by one of two things:

  1. The new value that you want is what the PropertyGrid control considers it to be the default value of the property.
  2. The property has the attribute DesignerSerializationVisibilityAttribute set to Hidden .
Question:  Whenever you set the Red color (or any other color), do you get the color name in bold letters in the property grid?  If not, then the property grid is assuming it is the property's default value and #1 above applies.

To solve #1, use the DefaultValueAttribute attribute in the BackColor property and set it to whatever value you set for the private variable backColor in the constructor of the class.

To determine if #2 applies, right-click the base class name and select "Go to definition".  A new code window opens showing the base class declaration.  Look for the BackColor property and see if it has the DesignerSerializationVisibilityAttribute attribute applied.  If yes, then re-apply the attribute in your BackColor property with a value of Visible .
MCP
webJose  Monday, October 05, 2009 5:37 PM
i tried by using

protected override void OnPaint(PaintEventArgs e)
        {
            OnDrawBackground(e.Graphics, base.BackColor);
            base.OnPaint(e);
        }

but sense nothing
Koray Samsun  Monday, October 05, 2009 5:48 PM
try

[

Browsable(true)]

[

Category("Colors")]

 

public override Color BackColor

{

 

get { return backColor; }

 

set

{

 

if (backColor == value)

 

return;

backColor =

value;

 

base.BackColor=value;<--new line

Invalidate();

}

}

Tamer Oz  Monday, October 05, 2009 5:50 PM
thanks. i applied DesignerSerializationVisibilityAttribute as Visible but nothing changed still it does not affect design class
also applied DefaultValueAttribute but same
Koray Samsun  Monday, October 05, 2009 5:50 PM
thanks Tamer, i applied base.BackColor=value;<--new line
but it changed nothing. Now i dont think its because of base.BackColor.
The problem is in design time, changes in property window for backcolor does not add any code piece to design class.

Because when i add myControl.BackColor = Color.Red on forms Load event it works.
Koray Samsun  Monday, October 05, 2009 5:54 PM
Did you compile the project and then set the property again after changing the code.

Since you set the base's backcolor it should serialize.

I reproduced error on my machine and solved it by this way.
  • Marked As Answer byKoray Samsun Monday, October 05, 2009 6:10 PM
  •  
Tamer Oz  Monday, October 05, 2009 6:00 PM
Thank you Tamer.
My class was in a dll project. And i'm using it in a sample Winform application. Seem that recompiling dll projectand then recompiling winform project has some trick. So i create a new winform project to test recent changes on dll project and it worked thank a lot.
Koray Samsun  Monday, October 05, 2009 6:12 PM

You can use google to search for other answers

Custom Search

More Threads

• ClickOnce Start Menu Short cuts
• Determine when a program runs at system startup
• DragDrop - figuring out who the sender is
• Condensing dc.DrawString() text to fit rectangle region
• fixed row datagridview
• What is the best practice to autosize the button which anchors to the right side of the window?
• dll references
• Toolstrip question
• Safe Source ?
• Finding New XP Icons...