c# - Pass properties of parent control to children controls -


i developing set of custom controls specific application. want define properties universal on set of controls appearance purposes, argument's sake let's make customctrl.accentcolor

i want define same property windows form i.e. form1.accentcolor , when change it, custom controls' accentcolor should change, when change forecolor of form, labels' , buttons' etc forecolor changes it.

is @ possible or have settle effort of looping through custom controls , changing one-by-one?

short answer

since can have common base class controls mentioned in comments, option can create base class , add properties behavior ambient properties (like font) base control class.


detailed answer

an ambient property property on control that, if not set, retrieved parent control.

in our implementation, value parent form using findform method. in implementation, when getting property value, check if value equals default value , if parent has same property, return property value of parent form, otherwise return property value of control itself.

after adding xxxx property, in scenario should implement shouldserializexxxx , resetxxxx methods let designer when serialize property , how reset value when right click on property , choose reset.

mybasecontrol

using system.drawing; using system.windows.forms; public class mybasecontrol : control {     public mybasecontrol()     {         accentcolor = color.empty;     }     private color accentcolor;     public color accentcolor     {                 {             if (accentcolor == color.empty && parentformhasaccentcolor())                 return getparentformaccentcolor();             return accentcolor;         }         set         {             if (accentcolor != value)                 accentcolor = value;         }     }     private bool parentformhasaccentcolor()     {         return this.findform() != null &&                this.findform().gettype().getproperty("accentcolor") != null;     }     private color getparentformaccentcolor()     {         if (parentformhasaccentcolor())             return (color)this.findform().gettype()                                .getproperty("accentcolor").getvalue(this.findform());         else             return color.red;     }     private bool shouldserializeaccentcolor()     {         return this.accentcolor != getparentformaccentcolor();     }     private void resetaccentcolor()     {         this.accentcolor = getparentformaccentcolor();     } } 

mybaseform

public class baseform : form {     [defaultvalue("red")]     public color accentcolor { get; set; }     public baseform()     {         this.accentcolor = color.red;     } } 

form1

public partial class form1 : baseform {     public form1()     {         initializecomponent();     } } 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -