Convert string to font

  • Thread starter Thread starter Wim
  • Start date Start date
W

Wim

I would like to store the ListView font in the XML config file. So I have
a string variable listFont that stores the font (font.ToString()). It
must be a string variable because a Font object cannot be written to the
XML config file. Next time the program is started the font is read from
the config file. But how to convert the string to a Font object?
 
Hi Wm,

Wim said:
I would like to store the ListView font in the XML config file. So I have
a string variable listFont that stores the font (font.ToString()). It
must be a string variable because a Font object cannot be written to the
XML config file. Next time the program is started the font is read from
the config file. But how to convert the string to a Font object?

An example of converting a font to/from a string:

...
FontDialog fd = new FontDialog();

if(fd.ShowDialog() != DialogResult.Cancel )
{
Font font = fd.Font;
Console.WriteLine("Font: {0}", font.ToString());

TypeConverter tc = TypeDescriptor.GetConverter(typeof(Font));
string fontString = tc.ConvertToString(font);
Console.WriteLine("Font as string: {0}", fontString);

Font newFont = (Font)tc.ConvertFromString(fontString);
Console.WriteLine("Font: {0}", newFont.ToString());
}
...

Regards,
Dan
 
Why you don't save the Font propriety?

If you use the following constructor, save the familyName and the emSize in
the config file.

Font.Font(string familyName,float emSize)
 

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

Back
Top