Loading fonts from the registry?

aewarnick

Senior Contributor
Joined
Jan 29, 2003
Messages
1,031
this.fontDialog.Font=new Font((string)CommunicationLog.settings.GetValue("TitleFont.Name"),
(float)CommunicationLog.settings.GetValue("TitleFont.Size"),
(FontStyle)CommunicationLog.settings.GetValue("TitleFont.Style"));

That does not work, conversion error and it would be nice if I could save and load everything in one package. It can also be saved to the registry like this really easily but I cannot load it at all (it seems) this way:

[Font: Name=Bangle, Size=9.75, Units=3, GdiCharSet=0, GdiVerticalFont=False]

Help!
 
Does anyone know of any classes or methods I could use to convert Font values and Font.Style values to Fonts?
 
public static FontStyle MakeStyle(object fontstyle)
{
char[]c={','};
string style=(string)fontstyle; style=style.Replace(" ", "");;
string[]Style=style.Split(c, 6);
FontStyle FS=new FontStyle();
for(int i=0; i<Style.Length; i++)
{
if(Style=="Bold") FS=FS^FontStyle.Bold;
if(Style=="Italic") FS=FS^FontStyle.Italic;
if(Style=="Italic") FS=FS^FontStyle.Italic;
if(Style=="Regular") FS=FS^FontStyle.Regular;
if(Style=="Strikeout") FS=FS^FontStyle.Strikeout;
if(Style=="Underline") FS=FS^FontStyle.Underline;
}
return FS;
}

Now you know one. I just made this and it woks great.
When a FontStyle value is saved using Font.Style it looks like this: Bold, Italic, Underline, Strikeout
All you do is send the above method that object and it will return a FontStyle object that you can use in any "new" Font declaration as the style parameter.

For example:

this.label1.Font=new Font ("Verdana", 10.5, MakeStyle (YourRegistryKeyName.GetValue("FontStyleData")));
 
Last edited:
Here is another one to set the font from the property value: Font

the call:
this.label1.Font=MakeFont(YourRegistryKeyName.GetValue("TitleFont"));

the method:
public static Font MakeFont(object font)
{
char[]c = {'=', ','};
string sfont = (string)font;
string[]S = sfont.Split(c, 20);
Font NewFont = new Font(S[1], Convert.ToSingle(S[3]));
return NewFont;
}
 
Both put together!!!

public static Font MakeFont(object font, object fontstyle)
{
char[]c = {'=', ','};
string sfont = (string)font;
string[]S = sfont.Split(c, 20);

char[]d={','};
string style=(string)fontstyle; style=style.Replace(" ", "");;
string[]Style=style.Split(d, 6);
FontStyle FS=new FontStyle();
for(int i=0; i<Style.Length; i++)
{
if(Style=="Bold") FS=FS^FontStyle.Bold;
if(Style=="Italic") FS=FS^FontStyle.Italic;
if(Style=="Italic") FS=FS^FontStyle.Italic;
if(Style=="Regular") FS=FS^FontStyle.Regular;
if(Style=="Strikeout") FS=FS^FontStyle.Strikeout;
if(Style=="Underline") FS=FS^FontStyle.Underline;
}

Font NewFont = new Font(S[1], Convert.ToSingle(S[3]), FS);
return NewFont;
}
 
Use this one instead:
Change: used | instead of ^.

public static Font MakeFont(object font, object fontstyle)
{
char[]c = {'=', ','};
string sfont = (string)font;
string[]S = sfont.Split(c, 20);

char[]d={','};
string style=(string)fontstyle; style=style.Replace(" ", "");;
string[]Style=style.Split(d, 6);
FontStyle FS=new FontStyle();
for(int i=0; i<Style.Length; i++)
{
if(Style=="Bold") FS=FS | FontStyle.Bold;
if(Style=="Italic") FS=FS | FontStyle.Italic;
if(Style=="Italic") FS=FS | FontStyle.Italic;
if(Style=="Regular") FS=FS | FontStyle.Regular;
if(Style=="Strikeout") FS=FS | FontStyle.Strikeout;
if(Style=="Underline") FS=FS | FontStyle.Underline;
}

Font NewFont = new Font(S[1], Convert.ToSingle(S[3]), FS);
return NewFont;
}
 
Back
Top