Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Label control behaves differently at design time vs. run time

I am creating a custom Label control (by simply Inheriting the standard Label control and re-painting the background and text) because I need a very specific background and border. In the constructor of the control, I set the AutoSize property to false so I can have a standard default size for the new label.

 Public Sub New()

    'Set the default size of the control to 75x24
    Me.Height = 24
    Me.Width = 75

    'Turn off the autosize property.
    Me.AutoSize = False

    'Turn on double-buffering.
    Me.DoubleBuffered = True

 End Sub

In my application that uses this control, if I create the new custom label at run time (in code), the AutoSize property stays False, and it works properly.

If I try to add the new custom label to my form at design time, it comes in with the AutoSize property set to True, and I have to manually set it to False in the properties window. It's not a huge problem, but I don't understand why the behavior is different.

Any ideas what is causing this difference in behavior?

like image 792
Stewbob Avatar asked Jul 08 '09 14:07

Stewbob


People also ask

What is the use of label control explain its different properties?

The Label control represents a standard Windows label. It is generally used to display some informative text on the GUI which is not changed during runtime. Let's create a label by dragging a Label control from the Toolbox and dropping it on the form.

What is the default property of label control?

The default property for a Label is the Caption property. The default event for a Label is the Click event.

Is label a control?

A label is a graphical control element which displays text on a form. It is usually a static control; having no interactivity. A label is generally used to identify a nearby text box or other widget.


3 Answers

In your label class, you should override the AutoSize property.

//(In C#)
[System.ComponentModel.Browsable(false)]
[System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
public new bool AutoSize
{
   get { return base.AutoSize; }
   set { base.AutoSize = value; }
}

The browsable(false) will hide the property at design time and the DesignerSerializationVisibility attribute will tell the designer to not write any code into your designer file.

like image 73
NascarEd Avatar answered Oct 27 '22 04:10

NascarEd


I finally got this to work in VB. I had to disable the Set statement, essentially turning the Overridden AutoSize Property into a read-only property.

     Public Overrides Property AutoSize() As Boolean
        Get
           Return MyBase.AutoSize
        End Get
        Set(ByVal value As Boolean)
           'Do nothing here
        End Set
     End Property

Thanks to NascarEd for getting me pointed in the right direction.

like image 36
Stewbob Avatar answered Oct 27 '22 05:10

Stewbob


Just for your future info, to set the autosize property to False in the properties window, you need to set an attribute:-

<System.ComponentModel.DefaultValue(False)> _

Public Overrides Property AutoSize() As Boolean ....

like image 23
Jules Avatar answered Oct 27 '22 06:10

Jules