Problems with properties of derived controls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Q2hyaXN0aWFuIFdlaW5lcnQ=?=

    Problems with properties of derived controls

    Hello,
    I currently fight with a problem during the derivative of WinForm controls.

    In Visual Studio I created a new User Control. This control is derived from
    the DataGridView of the System.Windows. Forms namespace. I want to use this
    control as a template for futher controls. Within this control i want to set
    some property values to be pre-defined for further derived controls.

    My base class looks like this:

    public partial class BaseDataGrid : System.Windows. Forms.DataGridV iew
    {
    private bool _Enabled;

    public BaseDataGrid() : base()
    {
    InitializeCompo nent();
    this.Enabled = false;
    }

    [DefaultValue(fa lse)]
    public new bool Enabled
    {
    get
    {
    return this._Enabled;
    }
    set
    {
    this._Enabled = value;
    }
    }
    }

    For testing this control I created a WinForm and put it on the form. Looking
    at the property window shows the Enabled-Property as set to "true". and not
    as expected to "false". The entry "true" is bold, so the Designer recognized
    that the default value for this property is "false".

    Further, I derived a new User Control from my BaseDataGridVie w control, put
    it on my form. As the BaseDataGridVie w the property window shows the
    Enabled-Property as set to "true".

    It seems that the Designer uses the Property-Value of the next-higher level
    control in a control hierarchy as the default value for this property. But,
    for pre-defining the property it uses the property default value of the
    highest class in the object hierarchy.

    My second approch was not to "override" the Enabled-Property. I only set the
    Enabled-property derived from the DataGridView to "false".

    My BaseDataGrid-class now looks like this:

    public partial class BaseDataGrid : System.Windows. Forms.DataGridV iew
    {
    public BaseDataGrid() : base()
    {
    this.Enabled = false;
    }
    }

    One again, putting this control on a WinForm is suprising me. Within the
    property window the Enabled-property is set to false. Everything seems to be
    fine. But when changing it to "true" there will be not code is written into
    the InitializeCompo nent-method of the form. When starting the form the
    control will be disabled. Debugging the InitializeCompo nent-method shows that
    the control has its Enabled-property set to "false".

    Part of the InitializeCompo nent-method when putting the control on the form:

    this.baseDataGr id1.ColumnHeade rsHeightSizeMod e =
    System.Windows. Forms.DataGridV iewColumnHeader sHeightSizeMode .AutoSize;
    this.baseDataGr id1.Enabled = false;
    this.baseDataGr id1.Location = new System.Drawing. Point(358, 88);
    this.baseDataGr id1.Name = "baseDataGrid1" ;
    this.baseDataGr id1.Size = new System.Drawing. Size(240, 150);
    this.baseDataGr id1.TabIndex = 0;

    Part of the InitializeCompo nent-method after setting the property to true:

    this.baseDataGr id1.ColumnHeade rsHeightSizeMod e =
    System.Windows. Forms.DataGridV iewColumnHeader sHeightSizeMode .AutoSize;
    this.baseDataGr id1.Location = new System.Drawing. Point(358, 88);
    this.baseDataGr id1.Name = "baseDataGrid1" ;
    this.baseDataGr id1.Size = new System.Drawing. Size(240, 150);
    this.baseDataGr id1.TabIndex = 0;



    Hope, anyone of you has an idea how to solve this problem.

    Thanks and best regrads
    Chris
  • =?Utf-8?B?Q2hyaXN0aWFuIFdlaW5lcnQ=?=

    #2
    Re: Problems with properties of derived controls

    Hello Ignacio,
    thanks for Your reply. I was enjoying the good weather this weekend, so
    sorry for my late delay.

    First of, thanks for the important information about the fact, that a
    pre-definition of a control property doesn't be used by the designer.

    Your solution, to assign the desired values within the constructor was the
    point where my problems began. I have changed my example to make my problem a
    litte bit more "visual".

    Lets take a textbox and build a new textbox control to be the base for all
    further textboxes. Lets definie that the default back color of the textbox
    should be blue.

    The source code of the text box:

    public partial class BaseTextBox : TextBox
    {
    public BaseTextBox()
    :base()
    {
    InitializeCompo nent();
    this.BackColor = Color.Blue;
    }
    }

    I put this control on a WinForm. The back color of the control is blue.
    Within the property window, the back color property is set to blue.
    Everythink seems to be fine. But the setting is bold, so when saving the
    form, it will be written to the InitializeCompo nent method.

    The InitializeCompo nent method of the form:

    private void InitializeCompo nent()
    {
    this.baseTextBo x1 = new ControlInherita nce.BaseTextBox ();
    this.SuspendLay out();
    //
    // baseTextBox1
    //
    this.baseTextBo x1.BackColor = System.Drawing. Color.Blue;
    this.baseTextBo x1.Location = new System.Drawing. Point(12, 25);
    this.baseTextBo x1.Name = "baseTextBo x1";
    this.baseTextBo x1.Size = new System.Drawing. Size(100, 20);
    this.baseTextBo x1.TabIndex = 0;
    //
    // Form1
    //
    this.AutoScaleD imensions = new System.Drawing. SizeF(6F, 13F);
    this.AutoScaleM ode = System.Windows. Forms.AutoScale Mode.Font;
    this.ClientSize = new System.Drawing. Size(292,266);
    this.Controls.A dd(this.baseTex tBox1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayo ut(false);
    this.PerformLay out();
    }

    When understanding the use polymorphism right, changes at a class will take
    affect everywhere, where it has been used. So lets try it. Lets say, the
    default back color of our textbox should be red. First I will change the base
    class code as follows:

    public partial class BaseTextBox : TextBox
    {
    public BaseTextBox()
    :base()
    {
    InitializeCompo nent();
    this.BackColor = Color.Red;
    }
    }

    When I open the form I already put the "former" base textbox on it, the
    changes will not take affect: The old base text box is still blue, a new one
    will be red...

    This all happens because of the designer didn't recognize that we have
    changed the "default" back color. The designer notice that the back color has
    been changed from its default (as far as I know: SystemColor.Win dow) to the
    new value blue. So You have to replace every used base textbox with its new
    version :-(

    A side effect I notice: Take the base textbox and change the bakc color to
    the default (SystemColor.Wi ndow). The designer will never accepts any color
    as default value...

    BUT: this code will work fine...

    public partial class BaseTextBox : TextBox, ISupportInitial ize
    {
    public BaseTextBox()
    : base()
    {
    InitializeCompo nent();
    this.BackColor = Color.Blue ;
    }

    [DefaultValue(ty peof(Color), "Blue")]
    public new Color BackColor
    {
    get
    {
    return base.BackColor;
    }
    set
    {
    base.BackColor = value;
    }
    }

    public void BeginInit()
    {
    }

    public void EndInit()
    {
    Color c = this.BackColor;
    }
    }

    The textbox on the form is blue. Replace Blue with Red and rebuild Your
    projekt. Now the textbox will be red... But this is quit a dirty way to do
    this...


    Perhaps, I have a big error within my thinking, but hope anyone can help me...

    Thanks
    Chris



    Comment

    Working...