[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?
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.
Re: [2005] Custom Property Default Value for fonts
Quote:
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.
Quote:
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.
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'.
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! :D
Re: [2005] Custom Property Default Value for fonts
Quote:
Originally Posted by
Tomazas
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! :D
Nice. I stand corrected.