Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
When creating a custom control (UserControl) the IDE gives you a standard Designer <usercontrol>.Designer.cs for your <usercontrol>.cs. I'm trying to understand how the designer knows which class members to initialize in InitializeComponent(). When I assign an event handler to and event what mechanism in Designer accomplishes this. I have a string member propert Text that is not being updated at design time.

this of course is the <usercontrol>.Designer.cs
C#
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
   this.SuspendLayout();
   //
   // SW
   //
   this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
   this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
   this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
   this.Name = "SW";
   this.Size = new System.Drawing.Size(39, 42);
   this.Paint += new System.Windows.Forms.PaintEventHandler(this.BitSW_Paint);
   this.Resize += new System.EventHandler(this.BitSW_Resize);
   this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.BitSW_MouseUp);
   this.ResumeLayout(false);
}

the UserControl.Text is overridden
C#
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[Description("Text label for this control.")]
public override string Text
{
   get
   {
      return base.Text;
   }
   set
   {
      if (value != base.Text)
      {
         base.Text = value;
         Invalidate();
      }
   }
}


Thanks guys and gals any help is greatly.
Posted
Updated 26-Jan-10 11:41am
v2

All public properties that have a setter are serialized to the designer file so long as they aren't decorated with
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

If a property has a default value (by either DefaultValue attribute or ShouldSerializePropertyName and ResetPropertyName) then it won't be serialized if it's current value == the default value.
 
Share this answer
 
If you goto the definition for UserControl you will see that the Text property is like this:
C#
[Bindable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override string Text { get; set; }
In your override, do this
C#
[EditorBrowsable(EditorBrowsableState.Advanced)]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
    get { return base.Text; }
    set
    {
        if (value != base.Text)
        {
            base.Text = value;
            Invalidate();
        }
    }
}
 
Share this answer
 
v2
C#
this.sw1.AlignText = System.Drawing.ContentAlignment.MiddleCenter;
this.sw1.BackColor = System.Drawing.Color.Black;
this.sw1.BackOff = System.Drawing.Color.Black;
this.sw1.BackOn = System.Drawing.Color.Lime;
this.sw1.bIsOn = false;
this.sw1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.sw1.ForeColor = System.Drawing.Color.Lime;
this.sw1.ForeOff = System.Drawing.Color.Lime;
this.sw1.ForeOn = System.Drawing.Color.Black;
this.sw1.Location = new System.Drawing.Point(137, 81);
this.sw1.Name = "sw1";
this.sw1.Size = new System.Drawing.Size(20, 74);
this.sw1.TabIndex = 0;

Something has changed then because there is no initializer for the Text property. Now I did override the UserControl.Text property
C#
/// <summary>
/// Text label for this control."
/// </summary>
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[Description("Text label for this control.")]
public override string Text
{
   get
   {
      return base.Text;
   }
   set
   {
      if (value != base.Text)
      {
         base.Text = value;
         Invalidate();
      }
   }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900