Saving and restring Font info

B

Bill Angus

I'm having trouble with windows forms/controls and fonts. I want to allow
font selection for a text-box, and then I want to save the user-selected
font to a database. None of my database field-types obviously is "font". So
I set up the database to store:
-font_family_name as string
-emSize as integer
-font_Style as integer

When I went try to save and restore the user-selected font from the
database, I run into troubles. Fontname, size and style are read-only
properties. I find that I cannot set the font of a control programmatically
given its name size and style...

Any idea how one may save and restore font info? (... from a database
table -- NOT from the My.Settings namespace which I already know how to
do)...

Thanks and have a great day!

Bill Angus, MA
http://www.psychtest.com
 
B

Brian Schwartz

You have to create a new font object:
textBox1.Font = new Font(...etc...);
There are several different overloads of the font constructor you can choose
from.
 
B

Bill Angus

I discovered what you say here about font assignment in the docs... but I think there is a bug in VB 2005 here....

When I try to set the font with variables retrieved from a datastore, the font(x,x,x) statement produces an error ... overloaded resolution fails because a narrowing convention is needed.

Thanks Brian and have a great day!

Bill Angus, MA
http://www.psychtest.com
You have to create a new font object:
textBox1.Font = new Font(...etc...);
There are several different overloads of the font constructor you can choose
from.
 
B

Brian Schwartz

Have you casted the objects you're pulling out of the data store to their
specific types? For example, when you grab a field from a data reader, it is
an object by default, unless you specifically ask for it as, or cast it to,
a specific type.

--
Brian Schwartz
FishNet Components
http://www.fishnetcomponents.com
Fish Grid .NET Light: Powerful Layouts for Small Datasets


I discovered what you say here about font assignment in the docs... but I
think there is a bug in VB 2005 here....

When I try to set the font with variables retrieved from a datastore, the
font(x,x,x) statement produces an error ... overloaded resolution fails
because a narrowing convention is needed.

Thanks Brian and have a great day!

Bill Angus, MA
http://www.psychtest.com
You have to create a new font object:
textBox1.Font = new Font(...etc...);
There are several different overloads of the font constructor you can choose
from.
 
B

Bill Angus

I went back to the docs and found that it should be possible to convert "most" objects to and from string (i.e. string is a great format for storing and retrieving from a datastore).

I set a line of code as follows which provides a string representation of a font ....
Dim strFont As String = Me.FontDialog1.Font.ToString

The string which is provided stores easily in the SQL datastore. It was actually stored as follows....
'Size=18, Units=3, GdiCharSet=0, GdiVerticalFont=False]'

With the above value stored in strFont, I get a failure upon trying to restore the font from string.

the code for restoring is as follows...

Me.FontDialog1.Font = CType(TypeDescriptor.GetConverter(GetType(Font)).ConvertFromString(strFont), Font)

ConvertFromString fails with the message: Value of 'Size=18, Units=3, GdiCharSet=0, GdiVerticalFont=False]' is not valid for 'units'.

this really does seem to be either a bug in the documentation or a bug in the product... I still can't get a valid value for "string representation of font" and then to restore a font setting from it.

Thanks and have a great day!

Bill Angus, MA
http://www.psychtest.com
Have you casted the objects you're pulling out of the data store to their
specific types? For example, when you grab a field from a data reader, it is
an object by default, unless you specifically ask for it as, or cast it to,
a specific type.

--
Brian Schwartz
FishNet Components
http://www.fishnetcomponents.com
Fish Grid .NET Light: Powerful Layouts for Small Datasets


I discovered what you say here about font assignment in the docs... but I
think there is a bug in VB 2005 here....

When I try to set the font with variables retrieved from a datastore, the
font(x,x,x) statement produces an error ... overloaded resolution fails
because a narrowing convention is needed.

Thanks Brian and have a great day!

Bill Angus, MA
http://www.psychtest.com
You have to create a new font object:
textBox1.Font = new Font(...etc...);
There are several different overloads of the font constructor you can choose
from.
 
B

Bill Angus

Duhhhh.... got it finally ... The saving of a font to string and recreating
from string works if the string is created using the following:

Dim strFont As String =
TypeDescriptor.GetConverter(tbHeader.Font).ConvertToString(tbHeader.Font)
Dim strColor As String =
TypeDescriptor.GetConverter(tbHeader.ForeColor).ConvertToString(tbHeader.For
eColor)

....and restored from the strFont string using the following...

Try
Me.tbHeader.Font = _

CType(TypeDescriptor.GetConverter(GetType(Font)).ConvertFromString(strFont),
Font)
Me.tbHeader.ForeColor = _

CType(TypeDescriptor.GetConverter(GetType(Color)).ConvertFromString(strColor
), Color)
Catch ex As Exception
End Try

--
Tests described in this email are priced in Canadian funds (and do not
include shipping charges). To get a US amount divide by 1.1 approximately.

Thanks for using our service and have a great day!

Bill Angus, MA
http://www.psychtest.com
Actually the string that was stored is as follows...

'[Font: Name=Mona Lisa Recut, Size=18, Units=3, GdiCharSet=0,
GdiVerticalFont=False]'

Bill Angus, MA
http://www.psychtest.com
 

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