|
-
Jun 22nd, 2007, 08:53 PM
#1
[2005] Set the default value of a property.
Hi guys, I am creating UserControl and I need to set a particular font as a default font for the control. I know that I need to use the “DefaultValue” class but all my tries give me no result. Does any one know how to do this? This is my code but it doesn’t work.
vb Code:
<DefaultValue(GetType(Font), "Engravers MT, 26.25pt, style=Bold"), Category("Appearance")> _
Public Overloads Property Font() As Font
Get
Return MyBase.Font
End Get
Set(ByVal value As Font)
MyBase.Font = value
End Set
End Property
-
Jun 22nd, 2007, 09:14 PM
#2
Re: [2005] Set the default value of a property.
Don't get confused between an "initial value" and a "default value". They are NOT the same thing. The initial value is the value a property has when an instance of the class is created. The default value is the value assigned to a property when the user right-clicks it in the Properties window and selects Reset. The two are completely unrelated and it is that second value that is controlled by the DefaultValue attribute.
It is impossible to set a default value for a property of type Font because default values can only be constant values. That means that you can only specify a literal or a constant as a default value. Try selecting any control in the designer, then open the Properties window and right-click the Font property. Notice that the Reset option is disabled? That's because there is no DefaultValue attribute for that property because it's not possible to set one.
Setting an initial value is something else entirely. Like I said, it's the value a property has when an instance is created. How do you set a property of an instance when it's created? In the constructor.
-
Jun 22nd, 2007, 09:43 PM
#3
Re: [2005] Set the default value of a property.
Hi jmcilhinney and thanks for the response. Yes I know that the initial value and the default values are deferent things. I set the initial value when I declare the private member variable in the User control class. For the font I just set the UserControls Font property in the design time. I came up reading about setting default value and I read the exact words that you have in your post “default values can only be constant values” but jmcilhinney for some reason the code on the bottom works even though a color type is not a constant!
vb Code:
''' <summary>
''' Gets or sets the second-hand color.
''' </summary>
<DefaultValue(GetType(Color), "ControlText"), Category("Appearance") _
, Description("Indicates the second-hand color.")> _
Public Property SecondHandColor() As Drawing.Color
Get
Return Me._secondHandPen.Color
End Get
Set(ByVal value As Drawing.Color)
If Me._secondHandPen.Color <> value Then
Me._secondHandPen.Color = value
Me.ClockPanel.Invalidate()
End If
End Set
End Property
I noticed that enum types work as well whit the same way. Are the colors enum values or constants?
Last edited by VBDT; Jun 22nd, 2007 at 09:55 PM.
-
Jun 22nd, 2007, 10:21 PM
#4
Re: [2005] Set the default value of a property.
'ControlText' is a member of the KnownColor enumeration. All members of all enumerations are defined as constant numerical values. Color is a special case because you can specify a constant KnownColor value and the translation to an actual Color is done for you. Try specifying a Color that doesn't correspond to a KnownColor value and you'll hit the same roadblock.
-
Jun 22nd, 2007, 11:09 PM
#5
Re: [2005] Set the default value of a property.
Thanks jmcilhinney for the help and info. I guess I’ll pass this one I just wonder how did they manage to set controls’ font default values when the control is drugged and dropped on the form?
-
Jun 23rd, 2007, 12:27 AM
#6
Re: [2005] Set the default value of a property.
 Originally Posted by VBDT
Thanks jmcilhinney for the help and info. I guess I’ll pass this one  I just wonder how did they manage to set controls’ font default values when the control is drugged and dropped on the form?
It's "dragged", not "drugged".
This goes back to EXACTLY what I said in the first place:
Don't get confused between an "initial value" and a "default value".
Try selecting any control in the designer, then open the Properties window and right-click the Font property. Notice that the Reset option is disabled? That's because there is no DefaultValue attribute for that property because it's not possible to set one.
Controls don't have any default value for their Font property. The initial value is set when the control is created but there is NO default value.
-
Jun 23rd, 2007, 12:46 AM
#7
Re: [2005] Set the default value of a property.
This goes back to EXACTLY what I said in the first place:
Exactly
jmcilhinney, Actually I tested your suggestion and the “Reset” option is enabled when the font isn’t Microsoft Sans Serif, 8.25pt font.
-
Jun 23rd, 2007, 12:51 AM
#8
Re: [2005] Set the default value of a property.
Hmmm... that I didn't notice. I'm guessing that that ability is specifically provided by the IDE and is related to the Control.DefaultFont property.
-
Jun 23rd, 2007, 12:56 AM
#9
Re: [2005] Set the default value of a property.
Yes I guess so. I also searched on the internet for a while hoping to find some lost example but there are none. Anyway thank you so much. And yes what is wrong with is rep stuff? Every time I try to give you one it doesn’t let me
-
Jun 23rd, 2007, 01:06 AM
#10
Re: [2005] Set the default value of a property.
Jmcilhinney, I just have one thought: could this property have a string type so that it sets the class member variable by converting the string into font?
-
Jun 23rd, 2007, 01:46 AM
#11
Re: [2005] Set the default value of a property.
I would guess not. You can create a Color from a single, constant value but there is no constructor or method that will create Font from a single string.
-
Jul 5th, 2007, 05:11 AM
#12
Re: [2005] Set the default value of a property.
Hi jmcilhinney, I think I found a way to do this. Here is the code that sets the UserControls default Font to "Times New Rom". I think I should post this for the people hwo will need it.
vb Code:
Public Class UserControl1
Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.Font = Me._defaultFont
End Sub
Private _defaultFont As Font = New Font("Times New Roman", CSng(8.25), FontStyle.Bold)
'UserControl1's Font property
Public Overrides Property Font() As Font
Get
Return MyBase.Font
End Get
Set(ByVal value As Font)
If value Is Nothing Then
MyBase.Font = Me._defaultFont
Else
MyBase.Font = value
End If
End Set
End Property
'ShouldSerialize for UserControl1's Font property
Public Function ShouldSerializeFont() As Boolean
Return Not Me.Font.Equals(Me._defaultFont)
End Function
'ResetFont for UserControl1's Font property
Public Overrides Sub ResetFont()
Me.Font = Nothing
End Sub
End Class
But know I have another problem; I am trying to have a property that has custom object. This object has X, Y properties like the Point object type. I could list the x and y values and even can change them when I click on the little + sing and change the values, but the problem is that I can’t change the values with out expending the property. It doesn’t let me to type on it. Should I use some other attributes in addition what I have? Here is the code so far.
vb Code:
''' <summary>
''' Gets or sets the numbers' offset from the center.
''' </summary>
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Layout"), _
Description("Indicates the numbers' offset from the center.")> _
Public Property NumberOffset() As Offset
Get
Return Me._numberOffset
End Get
Set(ByVal value As Offset)
If Not Me._numberOffset.Equals(value) Then
Me._numberOffset = value
Me.ClockPanel.Invalidate()
End If
End Set
End Property
<TypeConverterAttribute(GetType(System.ComponentModel.ExpandableObjectConverter))> _
Public Class Offset
Dim _x As Integer
Dim _y As Integer
Sub New()
Me._x = 0
Me._y = 0
End Sub
<Category("NumberOffset"), DefaultValue(0))> _
Property X() As Integer
Get
Return Me._x
End Get
Set(ByVal value As Integer)
If Me._x <> value Then
Me._x = value
End If
End Set
End Property
<Category("NumberOffset"), DefaultValue(0))> _
Property Y() As Integer
Get
Return Me._y
End Get
Set(ByVal value As Integer)
If Me._y <> value Then
Me._y = value
End If
End Set
End Property
Public Overrides Function ToString() As String
Return String.Format("{0}, {1}", New String() {Me.X, Me.Y})
End Function
End Class
Last edited by VBDT; Jul 5th, 2007 at 05:20 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|