Label Autosize Property... Possible bug??

J

Jordi Rico

Hi, I've made the next inherited class in Visual Studio 2005:

Public Class LabelEx
Inherits System.Windows.Forms.Label

Sub New()
MyBase.New()
Me.ForeColor = Color.Black
Me.AutoSize = False
Me.Height = 16
End Sub

<System.ComponentModel.DefaultValue(False)> _
Public Overrides Property AutoSize() As Boolean
Get
Return MyBase.AutoSize
End Get
Set(ByVal value As Boolean)
MyBase.AutoSize = value
End Set
End Property
End Class

The problem is that Autosize Property remains always as true when I go
to design mode and put my LabelEx from the ToolBox to the form. Looking
at the designer file, I can see as every property I have set to all my
other extended controls is correct, except from autosize for labels...
Any idea??? I've been unable to find this anywhere, and it looks like a
bug, doesn't it? By the way, this code works properly in VS 2003, and
although the default in 2003 is Autosize=false, if I put it to true, it
works with no problem, the designer sets the value I've said...
 
J

Jay B. Harlow [MVP - Outlook]

Jordi,
I noticed that the other day, it feels like a bug to me, as VS is "breaking"
the contract that System.ComponentModel.DefaultValue states.

I will see what I can find.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hi, I've made the next inherited class in Visual Studio 2005:
|
| Public Class LabelEx
| Inherits System.Windows.Forms.Label
|
| Sub New()
| MyBase.New()
| Me.ForeColor = Color.Black
| Me.AutoSize = False
| Me.Height = 16
| End Sub
|
| <System.ComponentModel.DefaultValue(False)> _
| Public Overrides Property AutoSize() As Boolean
| Get
| Return MyBase.AutoSize
| End Get
| Set(ByVal value As Boolean)
| MyBase.AutoSize = value
| End Set
| End Property
| End Class
|
| The problem is that Autosize Property remains always as true when I go
| to design mode and put my LabelEx from the ToolBox to the form. Looking
| at the designer file, I can see as every property I have set to all my
| other extended controls is correct, except from autosize for labels...
| Any idea??? I've been unable to find this anywhere, and it looks like a
| bug, doesn't it? By the way, this code works properly in VS 2003, and
| although the default in 2003 is Autosize=false, if I put it to true, it
| works with no problem, the designer sets the value I've said...
|
 
J

Jordi Rico

Hi Jay,
if you find something please tell me, I'm looking everywhere and cannot
find anything clear...
Thank you


Jay B. Harlow [MVP - Outlook] ha escrito:
 
M

Marina Levit [MVP]

Wouldn't you have to in your constructor set MyBase.AutoSize = False?

I thought the DefaultValue attribute just determines whether or not design
time code is generated when the property is the default value. When the
property is already the default value, it doesn't bother generating the line
of code, and the property value is not bold in the properties window. When
it's anything other then the default, only then does it bother generating a
line for it, and then it looks bold in the properties window so it's easier
to tell you've modified it.
 
A

Ahmed

I was able to duplicated with the VS 2005 Team Suite. I also added a
msgbox in the set method to monitor the value, ie

<System.ComponentModel.DefaultValue(False)> _
Public Overrides Property AutoSize() As Boolean
Get
Return MyBase.AutoSize
End Get
Set(ByVal value As Boolean)
msgbox(value)
MyBase.AutoSize = value
End Set
End Property


Wether the defaultvalue is true or false, I get two windows in design
time: first one says false, second one says true.
 
J

Jay B. Harlow [MVP - Outlook]

Marina,
He has Me.AutoSize = False in his constructor. His overridden Property then
does a MyBase.AutoSize = false. Net effect is the same thing.

| I thought the DefaultValue attribute just determines whether or not design
| time code is generated when the property is the default value.
Ah! There's the rub! or should I say there's the bug!

The DefaultValueAttribute determines whether or not design time code is
generated. The constructor determines the runtime code.


Well apparently in the case of Label, the new .NET 2.0 layout manager
decides to ignore both & applies AutoSize = True to the control...

A fellow MVP uses the following code as a workaround:

Public Class LabelEx
Inherits System.Windows.Forms.Label

Private m_inited As Boolean

Sub New()
MyBase.New()
Me.ForeColor = Color.Black
Me.AutoSize = False
Me.Height = 16
End Sub

Protected Overrides Sub InitLayout()
MyBase.InitLayout()
m_inited = True
End Sub


<System.ComponentModel.DefaultValue(False)> _
Public Overrides Property AutoSize() As Boolean
Get
Return MyBase.AutoSize
End Get
Set(ByVal value As Boolean)
If Me.DesignMode AndAlso Not m_inited Then Return
MyBase.AutoSize = value
End Set
End Property

End Class



--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Wouldn't you have to in your constructor set MyBase.AutoSize = False?
|
| I thought the DefaultValue attribute just determines whether or not design
| time code is generated when the property is the default value. When the
| property is already the default value, it doesn't bother generating the
line
| of code, and the property value is not bold in the properties window. When
| it's anything other then the default, only then does it bother generating
a
| line for it, and then it looks bold in the properties window so it's
easier
| to tell you've modified it.
|
| | > Hi, I've made the next inherited class in Visual Studio 2005:
| >
| > Public Class LabelEx
| > Inherits System.Windows.Forms.Label
| >
| > Sub New()
| > MyBase.New()
| > Me.ForeColor = Color.Black
| > Me.AutoSize = False
| > Me.Height = 16
| > End Sub
| >
| > <System.ComponentModel.DefaultValue(False)> _
| > Public Overrides Property AutoSize() As Boolean
| > Get
| > Return MyBase.AutoSize
| > End Get
| > Set(ByVal value As Boolean)
| > MyBase.AutoSize = value
| > End Set
| > End Property
| > End Class
| >
| > The problem is that Autosize Property remains always as true when I go
| > to design mode and put my LabelEx from the ToolBox to the form. Looking
| > at the designer file, I can see as every property I have set to all my
| > other extended controls is correct, except from autosize for labels...
| > Any idea??? I've been unable to find this anywhere, and it looks like a
| > bug, doesn't it? By the way, this code works properly in VS 2003, and
| > although the default in 2003 is Autosize=false, if I put it to true, it
| > works with no problem, the designer sets the value I've said...
| >
|
|
 
J

Jordi Rico

Thanks Jay, I'll try today and I'll tell how it works.

Jay B. Harlow [MVP - Outlook] ha escrito:
Marina,
He has Me.AutoSize = False in his constructor. His overridden Property then
does a MyBase.AutoSize = false. Net effect is the same thing.

| I thought the DefaultValue attribute just determines whether or not design
| time code is generated when the property is the default value.
Ah! There's the rub! or should I say there's the bug!

The DefaultValueAttribute determines whether or not design time code is
generated. The constructor determines the runtime code.


Well apparently in the case of Label, the new .NET 2.0 layout manager
decides to ignore both & applies AutoSize = True to the control...

A fellow MVP uses the following code as a workaround:

Public Class LabelEx
Inherits System.Windows.Forms.Label

Private m_inited As Boolean

Sub New()
MyBase.New()
Me.ForeColor = Color.Black
Me.AutoSize = False
Me.Height = 16
End Sub

Protected Overrides Sub InitLayout()
MyBase.InitLayout()
m_inited = True
End Sub


<System.ComponentModel.DefaultValue(False)> _
Public Overrides Property AutoSize() As Boolean
Get
Return MyBase.AutoSize
End Get
Set(ByVal value As Boolean)
If Me.DesignMode AndAlso Not m_inited Then Return
MyBase.AutoSize = value
End Set
End Property

End Class



--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Wouldn't you have to in your constructor set MyBase.AutoSize = False?
|
| I thought the DefaultValue attribute just determines whether or not design
| time code is generated when the property is the default value. When the
| property is already the default value, it doesn't bother generating the
line
| of code, and the property value is not bold in the properties window. When
| it's anything other then the default, only then does it bother generating
a
| line for it, and then it looks bold in the properties window so it's
easier
| to tell you've modified it.
|
| | > Hi, I've made the next inherited class in Visual Studio 2005:
| >
| > Public Class LabelEx
| > Inherits System.Windows.Forms.Label
| >
| > Sub New()
| > MyBase.New()
| > Me.ForeColor = Color.Black
| > Me.AutoSize = False
| > Me.Height = 16
| > End Sub
| >
| > <System.ComponentModel.DefaultValue(False)> _
| > Public Overrides Property AutoSize() As Boolean
| > Get
| > Return MyBase.AutoSize
| > End Get
| > Set(ByVal value As Boolean)
| > MyBase.AutoSize = value
| > End Set
| > End Property
| > End Class
| >
| > The problem is that Autosize Property remains always as true when I go
| > to design mode and put my LabelEx from the ToolBox to the form. Looking
| > at the designer file, I can see as every property I have set to all my
| > other extended controls is correct, except from autosize for labels...
| > Any idea??? I've been unable to find this anywhere, and it looks like a
| > bug, doesn't it? By the way, this code works properly in VS 2003, and
| > although the default in 2003 is Autosize=false, if I put it to true, it
| > works with no problem, the designer sets the value I've said...
| >
|
|
 
J

Jordi Rico

Ok,
it works great!
Thank you very much!

Jordi Rico ha escrito:
Thanks Jay, I'll try today and I'll tell how it works.

Jay B. Harlow [MVP - Outlook] ha escrito:
Marina,
He has Me.AutoSize = False in his constructor. His overridden Property then
does a MyBase.AutoSize = false. Net effect is the same thing.

| I thought the DefaultValue attribute just determines whether or not design
| time code is generated when the property is the default value.
Ah! There's the rub! or should I say there's the bug!

The DefaultValueAttribute determines whether or not design time code is
generated. The constructor determines the runtime code.


Well apparently in the case of Label, the new .NET 2.0 layout manager
decides to ignore both & applies AutoSize = True to the control...

A fellow MVP uses the following code as a workaround:

Public Class LabelEx
Inherits System.Windows.Forms.Label

Private m_inited As Boolean

Sub New()
MyBase.New()
Me.ForeColor = Color.Black
Me.AutoSize = False
Me.Height = 16
End Sub

Protected Overrides Sub InitLayout()
MyBase.InitLayout()
m_inited = True
End Sub


<System.ComponentModel.DefaultValue(False)> _
Public Overrides Property AutoSize() As Boolean
Get
Return MyBase.AutoSize
End Get
Set(ByVal value As Boolean)
If Me.DesignMode AndAlso Not m_inited Then Return
MyBase.AutoSize = value
End Set
End Property

End Class



--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Wouldn't you have to in your constructor set MyBase.AutoSize = False?
|
| I thought the DefaultValue attribute just determines whether or not design
| time code is generated when the property is the default value. When the
| property is already the default value, it doesn't bother generating the
line
| of code, and the property value is not bold in the properties window. When
| it's anything other then the default, only then does it bother generating
a
| line for it, and then it looks bold in the properties window so it's
easier
| to tell you've modified it.
|
| | > Hi, I've made the next inherited class in Visual Studio 2005:
| >
| > Public Class LabelEx
| > Inherits System.Windows.Forms.Label
| >
| > Sub New()
| > MyBase.New()
| > Me.ForeColor = Color.Black
| > Me.AutoSize = False
| > Me.Height = 16
| > End Sub
| >
| > <System.ComponentModel.DefaultValue(False)> _
| > Public Overrides Property AutoSize() As Boolean
| > Get
| > Return MyBase.AutoSize
| > End Get
| > Set(ByVal value As Boolean)
| > MyBase.AutoSize = value
| > End Set
| > End Property
| > End Class
| >
| > The problem is that Autosize Property remains always as true when I go
| > to design mode and put my LabelEx from the ToolBox to the form. Looking
| > at the designer file, I can see as every property I have set to all my
| > other extended controls is correct, except from autosize for labels...
| > Any idea??? I've been unable to find this anywhere, and it looks like a
| > bug, doesn't it? By the way, this code works properly in VS 2003, and
| > although the default in 2003 is Autosize=false, if I put it to true, it
| > works with no problem, the designer sets the value I've said...
| >
|
|
 
J

Jay B. Harlow [MVP - Outlook]

Thank you for following up that it worked!

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Ok,
| it works great!
| Thank you very much!
|
| Jordi Rico ha escrito:
|
| > Thanks Jay, I'll try today and I'll tell how it works.
| >
| > Jay B. Harlow [MVP - Outlook] ha escrito:
| >
| > > Marina,
| > > He has Me.AutoSize = False in his constructor. His overridden Property
then
| > > does a MyBase.AutoSize = false. Net effect is the same thing.
| > >
| > > | I thought the DefaultValue attribute just determines whether or not
design
| > > | time code is generated when the property is the default value.
| > > Ah! There's the rub! or should I say there's the bug!
| > >
| > > The DefaultValueAttribute determines whether or not design time code
is
| > > generated. The constructor determines the runtime code.
| > >
| > >
| > > Well apparently in the case of Label, the new .NET 2.0 layout manager
| > > decides to ignore both & applies AutoSize = True to the control...
| > >
| > > A fellow MVP uses the following code as a workaround:
| > >
| > > Public Class LabelEx
| > > Inherits System.Windows.Forms.Label
| > >
| > > Private m_inited As Boolean
| > >
| > > Sub New()
| > > MyBase.New()
| > > Me.ForeColor = Color.Black
| > > Me.AutoSize = False
| > > Me.Height = 16
| > > End Sub
| > >
| > > Protected Overrides Sub InitLayout()
| > > MyBase.InitLayout()
| > > m_inited = True
| > > End Sub
| > >
| > >
| > > <System.ComponentModel.DefaultValue(False)> _
| > > Public Overrides Property AutoSize() As Boolean
| > > Get
| > > Return MyBase.AutoSize
| > > End Get
| > > Set(ByVal value As Boolean)
| > > If Me.DesignMode AndAlso Not m_inited Then Return
| > > MyBase.AutoSize = value
| > > End Set
| > > End Property
| > >
| > > End Class
| > >
| > >
| > >
| > > --
| > > Hope this helps
| > > Jay B. Harlow [MVP - Outlook]
| > > .NET Application Architect, Enthusiast, & Evangelist
| > > T.S. Bradley - http://www.tsbradley.net
| > >
| > >
| > > | > > | Wouldn't you have to in your constructor set MyBase.AutoSize =
False?
| > > |
| > > | I thought the DefaultValue attribute just determines whether or not
design
| > > | time code is generated when the property is the default value. When
the
| > > | property is already the default value, it doesn't bother generating
the
| > > line
| > > | of code, and the property value is not bold in the properties
window. When
| > > | it's anything other then the default, only then does it bother
generating
| > > a
| > > | line for it, and then it looks bold in the properties window so it's
| > > easier
| > > | to tell you've modified it.
| > > |
| > > | | > > | > Hi, I've made the next inherited class in Visual Studio 2005:
| > > | >
| > > | > Public Class LabelEx
| > > | > Inherits System.Windows.Forms.Label
| > > | >
| > > | > Sub New()
| > > | > MyBase.New()
| > > | > Me.ForeColor = Color.Black
| > > | > Me.AutoSize = False
| > > | > Me.Height = 16
| > > | > End Sub
| > > | >
| > > | > <System.ComponentModel.DefaultValue(False)> _
| > > | > Public Overrides Property AutoSize() As Boolean
| > > | > Get
| > > | > Return MyBase.AutoSize
| > > | > End Get
| > > | > Set(ByVal value As Boolean)
| > > | > MyBase.AutoSize = value
| > > | > End Set
| > > | > End Property
| > > | > End Class
| > > | >
| > > | > The problem is that Autosize Property remains always as true when
I go
| > > | > to design mode and put my LabelEx from the ToolBox to the form.
Looking
| > > | > at the designer file, I can see as every property I have set to
all my
| > > | > other extended controls is correct, except from autosize for
labels...
| > > | > Any idea??? I've been unable to find this anywhere, and it looks
like a
| > > | > bug, doesn't it? By the way, this code works properly in VS 2003,
and
| > > | > although the default in 2003 is Autosize=false, if I put it to
true, it
| > > | > works with no problem, the designer sets the value I've said...
| > > | >
| > > |
| > > |
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top