persisting font info

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can anyone tell me how to persist and retrieve the font characteristics that
a user chooses to have displayed in a personalized label field on the front
of their application? I wanted to be able to give the user this ability so
that they could place a company logo, motto, or simply a message of the day
on the form, and have it redisplay from then on at start up just as it was to
begin with. I was trying to use an .rtf file to store the data. The place
I'm running into trouble is with the FontStyle. I can't reduplicate a
FontStyle of say, Bold/Italic/Underline. I can only reduplicate one of those
with a FontStyle fontstyle = FontStyle.Bold statement and then create a new
Font with a new Font(fontfamily, fontsize, fontstyle) statement. Any help
with this question would be greatly appreciated.
 
I dont quite understand why you are using .RTF to store the information. As
far as the FontStyle you can use OR because it supports bitmask. Ie. Font f =
new Font("Arial", 12F, FontStyle.Bold | FontStyle.Italic); Font is immutable
so you have to create a new object.
 
Thank you Alex. Can you suggest a better way of saving the font data? I
would really like to do this the best and most efficient way possible.

Cheers,
Derrick
 
RTF wasn't exactly intended for saving fonts. You have to go through a code
mess / hacks to get it to work. If I had to save some font data to disk I
would do it in an XML file.

Here's a piece of code for appsettings serializing:

using System;
using System.IO;
using System.Xml;

namespace Example
{
public class AppSettings
{
private XmlDocument xd = new XmlDocument();

public AppSettings()
{
string path = System.Windows.Forms.Application.StartupPath +
"\\appsettings.xml";
if (File.Exists(path))
xd.Load(path);
else
xd.LoadXml("<AppSettings></AppSettings>");
}

public string GetSetting(string settingPath)
{
try
{
string[] path = settingPath.Split(".".ToCharArray());
XmlElement current = xd.DocumentElement[path[0]];
for(int x = 1; x<path.Length; x++)
{
current = current[path[x]];
}
return current.InnerText;
}
catch
{
return "";
}
}
public void SetSetting(string settingPath, string val)
{
string[] path = settingPath.Split(".".ToCharArray());
XmlElement current;
if (xd.DocumentElement[path[0]] != null)
{
current = xd.DocumentElement[path[0]];
}
else
{
current = xd.CreateElement(path[0]);
xd.DocumentElement.AppendChild(current);
}
for(int x = 1; x<path.Length; x++)
{
if (current[path[x]] != null)
{
current = current[path[x]];
}
else
{
current.AppendChild(xd.CreateElement(path[x]));
current = current[path[x]];
}
}
current.InnerText = val;
return;
}
public void Save()
{
xd.Save(System.Windows.Forms.Application.StartupPath +
"\\appsettings.xml");
}
}
}
 
Hi Derrick,

There are at least two ways of doing this:
1. Store values for each argument used in the font constructor and it's type
in a config file. Retrieve them and cast them as require while calling the
constructor
2. Serialize the font object store it in a file. Deserialize the data when
required.

Let me know if you need more info.

HTH,
Rakesh Rajan
 
Thanks to the quick and thorough information you all gave I was able to do
what I wanted. I still have a lot of learning to do. I couldn't have done
it without your help. Thanks a ton!

Derrick

Alex Korchemniy said:
RTF wasn't exactly intended for saving fonts. You have to go through a code
mess / hacks to get it to work. If I had to save some font data to disk I
would do it in an XML file.

Here's a piece of code for appsettings serializing:

using System;
using System.IO;
using System.Xml;

namespace Example
{
public class AppSettings
{
private XmlDocument xd = new XmlDocument();

public AppSettings()
{
string path = System.Windows.Forms.Application.StartupPath +
"\\appsettings.xml";
if (File.Exists(path))
xd.Load(path);
else
xd.LoadXml("<AppSettings></AppSettings>");
}

public string GetSetting(string settingPath)
{
try
{
string[] path = settingPath.Split(".".ToCharArray());
XmlElement current = xd.DocumentElement[path[0]];
for(int x = 1; x<path.Length; x++)
{
current = current[path[x]];
}
return current.InnerText;
}
catch
{
return "";
}
}
public void SetSetting(string settingPath, string val)
{
string[] path = settingPath.Split(".".ToCharArray());
XmlElement current;
if (xd.DocumentElement[path[0]] != null)
{
current = xd.DocumentElement[path[0]];
}
else
{
current = xd.CreateElement(path[0]);
xd.DocumentElement.AppendChild(current);
}
for(int x = 1; x<path.Length; x++)
{
if (current[path[x]] != null)
{
current = current[path[x]];
}
else
{
current.AppendChild(xd.CreateElement(path[x]));
current = current[path[x]];
}
}
current.InnerText = val;
return;
}
public void Save()
{
xd.Save(System.Windows.Forms.Application.StartupPath +
"\\appsettings.xml");
}
}
}


Derrick said:
Thank you Alex. Can you suggest a better way of saving the font data? I
would really like to do this the best and most efficient way possible.

Cheers,
Derrick
 
Serializing sounds like a good way to go. Especially since the FontStyle has
no constructor. I'll need to spend some time researching how to do it this
way. The way I've been able to do so far is quite hacky. Thank you for the
great tip.

Derrick
 
Back
Top