Results 1 to 12 of 12

Thread: [2005] Set the default value of a property.

  1. #1

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    [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:
    1. <DefaultValue(GetType(Font), "Engravers MT, 26.25pt, style=Bold"), Category("Appearance")> _
    2.     Public Overloads Property Font() As Font
    3.         Get
    4.             Return MyBase.Font
    5.         End Get
    6.         Set(ByVal value As Font)
    7.             MyBase.Font = value
    8.         End Set
    9.     End Property

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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:
    1. ''' <summary>
    2.     ''' Gets or sets the second-hand color.
    3.     ''' </summary>
    4.     <DefaultValue(GetType(Color), "ControlText"), Category("Appearance") _
    5.     , Description("Indicates the second-hand color.")> _
    6.     Public Property SecondHandColor() As Drawing.Color
    7.         Get
    8.             Return Me._secondHandPen.Color
    9.         End Get
    10.         Set(ByVal value As Drawing.Color)
    11.             If Me._secondHandPen.Color <> value Then
    12.                 Me._secondHandPen.Color = value
    13.                 Me.ClockPanel.Invalidate()
    14.             End If
    15.         End Set
    16.     End Property
    I noticed that enum types work as well whit the same way. Are the colors enum values or constants?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Set the default value of a property.

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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

  10. #10

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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?

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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:
    1. Public Class UserControl1
    2.  
    3.     Sub New()
    4.  
    5.         ' This call is required by the Windows Form Designer.
    6.         InitializeComponent()
    7.  
    8.         ' Add any initialization after the InitializeComponent() call.
    9.         Me.Font = Me._defaultFont
    10.  
    11.     End Sub
    12.  
    13.     Private _defaultFont As Font = New Font("Times New Roman", CSng(8.25), FontStyle.Bold)
    14.  
    15.  
    16.     'UserControl1's Font property
    17.     Public Overrides Property Font() As Font
    18.         Get
    19.             Return MyBase.Font
    20.         End Get
    21.         Set(ByVal value As Font)
    22.             If value Is Nothing Then
    23.                 MyBase.Font = Me._defaultFont
    24.             Else
    25.                 MyBase.Font = value
    26.             End If
    27.         End Set
    28.     End Property
    29.  
    30.     'ShouldSerialize for UserControl1's Font property
    31.     Public Function ShouldSerializeFont() As Boolean
    32.         Return Not Me.Font.Equals(Me._defaultFont)
    33.     End Function
    34.  
    35.     'ResetFont for UserControl1's Font property
    36.     Public Overrides Sub ResetFont()
    37.         Me.Font = Nothing
    38.     End Sub
    39.  
    40. 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:
    1. ''' <summary>
    2.     ''' Gets or sets the numbers' offset from the center.
    3.     ''' </summary>
    4.     <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Layout"), _
    5.     Description("Indicates the numbers' offset from the center.")> _
    6.     Public Property NumberOffset() As Offset
    7.         Get
    8.             Return Me._numberOffset
    9.         End Get
    10.         Set(ByVal value As Offset)
    11.             If Not Me._numberOffset.Equals(value) Then
    12.                 Me._numberOffset = value
    13.                 Me.ClockPanel.Invalidate()
    14.             End If
    15.         End Set
    16.     End Property
    17.  
    18.  
    19.  <TypeConverterAttribute(GetType(System.ComponentModel.ExpandableObjectConverter))> _
    20.     Public Class Offset
    21.         Dim _x As Integer
    22.         Dim _y As Integer
    23.  
    24.         Sub New()
    25.             Me._x = 0
    26.             Me._y = 0
    27.         End Sub
    28.  
    29.         <Category("NumberOffset"), DefaultValue(0))> _
    30.         Property X() As Integer
    31.             Get
    32.                 Return Me._x
    33.             End Get
    34.             Set(ByVal value As Integer)
    35.                 If Me._x <> value Then
    36.                     Me._x = value
    37.                 End If
    38.             End Set
    39.         End Property
    40.  
    41.         <Category("NumberOffset"), DefaultValue(0))> _
    42.         Property Y() As Integer
    43.             Get
    44.                 Return Me._y
    45.             End Get
    46.             Set(ByVal value As Integer)
    47.                 If Me._y <> value Then
    48.                     Me._y = value
    49.                 End If
    50.             End Set
    51.         End Property
    52.  
    53.         Public Overrides Function ToString() As String
    54.             Return String.Format("{0}, {1}", New String() {Me.X, Me.Y})
    55.         End Function
    56.     End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width