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?
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.
The default property for a Label is the Caption property. The default event for a Label is the Click event.
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.
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.
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.
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 ....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With