Label management behaves differently during design and runtime.

I am creating a custom Label control (just inheriting the standard Label control and redrawing the background and text) because I need a special background and frame. In the control's constructor, I set the AutoSize property to false, so for the new label, I can have a default default size.

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 using this control, if I create a new custom shortcut at runtime (in code), the AutoSize property remains False and works correctly.

If I try to add a new custom label to my form at design time, it includes the AutoSize property set to True, and I need to manually set it to False in the properties window. This is not a huge problem, but I do not understand why the behavior is different.

Any ideas what causes this difference in behavior?

+6
visual-studio-2008 winforms
source share
4 answers

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 pointing in the right direction.

+3
source share

In your label class, you must 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; } } 

In the browser (false), the property will be hidden during development, and the DesignerSerializationVisibility attribute will instruct the developer not to write code in your designer file.

+6
source share

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

<System.ComponentModel.DefaultValue (False)> _

Public overrides the AutoSize () property as Boolean ....

+2
source share

If you switch to the development mode of a new control being created, you can select this control and change the properties as you would like. From now on, whenever you add this control to form (or another control), it will have the properties that you set there by default. This should allow you to set defaults as well as keep them visible so that developers can change things if they don't want to be changed in the future.

Also, check the code generated by the designer, as he will show you exactly what he did to generate the behavior you are looking for.

0
source share

All Articles