Having Problem with VS2005 and label.Autosize Behavior

ofern01

Member
Joined
Jan 3, 2006
Messages
9
Programming Experience
10+
I have a component class ( a label) created that use in all my projects. This Label has an specific size, color....
Now (in 2005) when I add it to a project, the Autosize is forced to True automatically and I have to manually change back to False in any project I add it.
Does anyone knows or have any idea of why?
 
Set autosize=false explicitly in your label usercontrol, either in Designer View or in code.

In code you have usually "Windows Form Designer generated code" region (if you didn't write it all yourself), and in there you find a method that initializes it, looking like this:
VB.NET:
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
 
End Sub
This is where you add your custom initialization of the control like this:

VB.NET:
'Add any ...
Mybase.AutoSize = False


Note, if you set some property for the control in Designer View, the generator will add this to an initialization area you can't modify in code editor, the generated code there may also look a little 'strange'. If you in Designer View now resets the preperty to default value, the generated initialization code is removed for you.
 
I have Tried both. Designer and Code.
This Works perfect in VS 2003. I have Tried it. In VS 2005 Does not. This is were I'm working now. I moved my application to 2005 and here is where I found this issue.
 
did you add (Mybase.AutoSize = False), recompiled your label control, then added your label control to a project, and it still sets autosize=true ?
 
BTW, When The object is added to a form, it sets the autosize = True. If I Reset it, it gives me the default size I seted on the class.
This is how it looks:
Partial Class safLblBase
Inherits System.Windows.Forms.Label
<System.Diagnostics.DebuggerNonUserCode()> _
Public Sub New()
MyBase.New()
'This call is required by the Component Designer.
InitializeComponent()
MyBase.AutoSize = False
End Sub
'Component overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.SuspendLayout()
'
'safLblBase
'
Me.Margin = New System.Windows.Forms.Padding(0)
Me.Size = New System.Drawing.Size(120, 21)
Me.Text = "LblBase"
Me.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
Me.ResumeLayout(False)
End Sub
Friend WithEvents safLblBase As System.Windows.Forms.Label
End Class

 
How very strange.. I was in VS2003 and it did what it was supposed to.
Now I tried it in VS05, and you're right - the AutoSize still is True.
I have to look more into this, if not others here know what's with it.

What get's me is that the property value is in bold font, meaning it is not the default value. There must be something in VS05 that overrides it.
 
The IDE is almost certainly setting that property after the object is created. You should override the AutoSize property so that you can control what it does. If you don't set the MyBase.AutoSize property in the Setter then if the IDE tries to set it it will have no effect.

Edit: Note that this means that you will never be able to change the AutoSize property from the outside unless you provide some other method.
 
I'm sure you must know what you said, but if read what I have done already, you can see that I have tried overriding this on the IDE, on the New(), initializecomponent().
Now, Where else can I Override this setting? It you can how me the code, it will be more helpfull than just saying it.
BTW, this works perfectly on VS2003. The problem is just on VS2005
 
ofern01 said:
you can see that I have tried overriding this on the IDE, on the New(), initializecomponent().
Now, Where else can I Override this setting?
You haven't OVERRIDDEN it anywhere. What you have done is SET it. To OVERRIDE a property, or any member for that matter, you have to provide a new implementation with the Overrides keyword:
VB.NET:
Public [U][B]Overrides[/B][/U] Property AutoSize() As Boolean
    Get
        'Get the property value from the base class.
        Return MyBase.AutoSize
    End Get
    Set(ByVal Value as Boolean)
        'The following line would set the property in the base class.
        'By not including it you prevent the base class property being set.
        'MyBase.AutoSize = Value
    End Set
End Property
Now whenever you, or the IDE, set AutoSize property of an instance of your class from the OUTSIDE it will have no effect, because the value is not passed to the base class. You can still set the property from INSIDE an instance by setting MyBase.AutoSize instead of Me.AutoSize.

This is an example of why it is important to use correct terminology. Using words that have specific meanings in the .NET context to mean something else is a good way to cause confusion.
 
Last edited:
My appologies. Your are right. I was so into changing the property setting that I did not use the correct meaning for Override in Net.
That Worked Pecfectly. Even Though, it should work that other way (changing the value) but no time for that. You gave me a solution and that is what matters.
Thanks....
 
Back
Top