views:

400

answers:

4

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?

+3  A: 

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.

NascarEd
I have implemented this, but even though the designer no longer explicitly sets the AutoSize property to true, like it was doing before, it still explicitly sets the .Size property as if AutoSize were still on. Maybe I'll trying overriding the Size property in my control too.
Stewbob
Even though this doesn't seem to work in VB, I appreciate your help. It has given me a better insight into creating custom controls. Thanks!
Stewbob
A: 

If you go into the design mode for the new control you are creating, you should be able to select that control and change the properties however you'd like. From that point on, whenever you add that control to form (or another control) it will have the properties you set there as the default. This should allow you to set defaults while also keeping them visible so that developers can change things should they not want it to be resizeable in the future.

Alternatively, check out the code generated by the designer, as it will show you exactly what it did to generate the behavior that you're looking for.

Kurisu
A: 

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.

Stewbob
+1  A: 

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 ....

Jules
Thanks, good info.
Stewbob