Results 1 to 6 of 6

Thread: [2005] Custom Property Default Value for fonts

  1. #1

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    [2005] Custom Property Default Value for fonts

    How would I set a Font Properties DefaultValue.

    I am currently using:

    Dim f as Font = New Font("Tahoma", 8.25!)

    then in the property:

    <DefaultValue(GetType(Font), "f")>

    But the property defaults to "Franklin Gothic Medium" when I rightclick it and select Reset.

    What am I doing wrong?
    Prefix has no suffix, but suffix has a prefix.

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

    Re: [2005] Custom Property Default Value for fonts

    It's important to differentiate between an initial value and a default value and also to identify what each is for and what each can do. The initial value of a property is the value that property has when the instance is first created. That is what I believe you want. The default value, as set by the DefaultValue attribute, is not necessarily the same as the initial value. The default value exists for a very specific purpose: to allow you to right-click a property in the designer and select "Reset". The initial value and the default value may be completely different.

    Also, it is not possible to set the default value to a variable. A properties default value must be a constant, which means it can only be one of the inbuilt VB.NET data types, e.g. String, Char, Integer, Boolean, Date, etc. It is not possible to set a default value for a property of type Font.
    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
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: [2005] Custom Property Default Value for fonts

    It is not possible to set a default value for a property of type Font.
    I figured it out. The reason it was showing "Franklin Gothic Medium" is because that is the only font on my system that starts with F.

    Here is the code that works:

    Code:
    <DefaultValue(GetType(Font), "Lucida Console")>
    That will make Lucida Console the default font for that property.

    That is what I believe you want.
    Sorry If I confused you. I do want the DefaultValue and not the initial value. I set all my initial values in Sub New. Is that the correct way to do it?

    So, I could only set the family of the DefaultValue and not the rest of the font properties. Any idea how those are set?

    Thank you again.
    Prefix has no suffix, but suffix has a prefix.

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

    Re: [2005] Custom Property Default Value for fonts

    Like I said, you can only set default values using constants. You can only pass literal values or declared constants to the DefaultValueAttribute constructor. There is no way to specify a literal Font and thus there's also no way to create a constant of type Font. Your other code was working because you were specifying the literal string "f". That literal had no connection whatsoever with your variable 'f'.
    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
    Member
    Join Date
    Aug 2006
    Posts
    34

    Thumbs up Re: [2005] Custom Property Default Value for fonts

    Yes, there is!
    At least in my VS2008 it works!
    Code:
        
        Private _MyFont As New Font("Times New Roman", 18, FontStyle.Bold)
    <CategoryAttribute("Application"), Browsable(True), _
            DescriptionAttribute("Application Font"), DefaultValue(GetType(Font), "Times New Roman,18,Bold")> _
        Public Property MyFont() As Font
            Get
                Return _MyFont
            End Get
            Set(ByVal value As Font)
                _MyFont = value
                Me.Invalidate()
            End Set
        End Property
    Work perfectly!

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

    Re: [2005] Custom Property Default Value for fonts

    Quote Originally Posted by Tomazas View Post
    Yes, there is!
    At least in my VS2008 it works!
    Code:
        
        Private _MyFont As New Font("Times New Roman", 18, FontStyle.Bold)
    <CategoryAttribute("Application"), Browsable(True), _
            DescriptionAttribute("Application Font"), DefaultValue(GetType(Font), "Times New Roman,18,Bold")> _
        Public Property MyFont() As Font
            Get
                Return _MyFont
            End Get
            Set(ByVal value As Font)
                _MyFont = value
                Me.Invalidate()
            End Set
        End Property
    Work perfectly!
    Nice. I stand corrected.
    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

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