Click here to Skip to main content
16,022,189 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
I have a problem, I want to be able to convert a string generated by the FontDialog window back into a font.

The thing is I have to store the information generated by the FontDialog into a Listbox. Listboxes only accepts Strings not fonts, therefore I have to convert the font into a string. That is no problem. The output of the font when converted to a string is as follows:

[Font: Name=Corbel, Size=14.25, Units=3, GdiCharSet=0, GdiVerticalFont=False]

Now what I want to do is convert the above string back into a font, however unlike converting integers to colors it seems alot harder and I am struggling.

Please can anyone shred some light and help me?

Thank-You :)
Posted
Updated 30-Dec-16 14:08pm

var fC = new FontConverter(); // <-= Cool Trick, now watch...

// From a string is:
PrintFont = fC.ConvertFromString(yourString) as Font;

// To a string is:
string yourString = fC.ConvertToString(PrintFont);


// NOW if you wanted to bring back the Script for said Font as well.
// there are a few more steps. First, you will have to Save the value.
PrintSettings.Default.GdiCharSet = _font.Dialog.Font.GdiCharSet;// byte Type.

// or
byte gdiValue = PrintFont.GdiCharSet; // something like that.

// OR, you can store it as an int with:
int gdiValue = PrintFont.GdiCharSet.GetHashCode();

// But whatever, the Script is not brought back in with the Font whether
// you save it as a text file string or Settings.setting, either way.
// byte or int. And _fontDialog.Font.GdiCharSet HAS NO SETTER. So you are
// pretty much screwed...(sic) ...unless you do it my way. LoL jk...

// So you have your PrintFont loaded back in using the above. But you want
// to also bring back the Script for the saved Font. Having saved that GdiCharSet
// value as well, you simply put them together. My method makes it as simple as:

PrintFont = GetFontWithScript();

// After this ^ call, all you need to do is assign it with:
_fontDialog.Font = PrintFont; // and you will be completely set.

// And here is how it puts the two together again.

private Font GetFontWithScript()
{
PrintFont = PrintSettings.Default.PrintFont;
var name = PrintFont.Name;
var size = PrintFont.Size;
var style = PrintFont.Style;
var unit = PrintFont.Unit;
var charSet = PrintSettings.Default.GdiCharSet;

return new Font(name,size,style,unit,charSet);
/*
* Font(String, Single, FontStyle, GraphicsUnit, Byte)
* Initializes a new Font using a specified size, style, unit and character set.
*
*/
}

// Also, to convert a color to a string and back is just as simple.
// Where...
System.Drawing.Color _yourColor; // Assign it from FontDialog or whatever...
// Then...
string colorString = _yourColor.ToArgb().ToString();

// Then, to convert a string to a Color just...
_yourColor = Color.FromArgb(Convert.ToInt32(colorString));

// or just work with the int or byte and forget the string. ;-)
 
Share this answer
 
v5
Comments
[no name] 31-Dec-16 3:43am    
Please do not post in old questions. This one is over 5 years old.
ZurdoDev 19-May-17 11:27am    
If the solution is good and has not already been provided, why not? Granted, haven't read this solution to know if it is good or not.
[no name] 19-May-17 12:30pm    
Took you long enough to find it.
ZurdoDev 19-May-17 13:16pm    
Ha! I just noticed that.
Somebody has created a FontConverter class that allows you to convert the Font to and from a string (the Font class is serializable). It's pretty simple... you just serialize to and deserialize from an XML string. There is another example in this article: .NET XML Serialization - a settings class.
 
Share this answer
 
String and Font has nothing to do in common, cannot be converted in any sensible way.

You could parse some text information and create a font based on it, see the class System.Drawing.Font and its constructors, http://msdn.microsoft.com/en-us/library/system.drawing.font.aspx[^].

—SA
 
Share this answer
 
v3
Comments
Nish Nishant 25-Apr-11 19:49pm    
Good answer. Voted 5.
Sergey Alexandrovich Kryukov 25-Apr-11 19:52pm    
Thank you, Nishant.
--SA
AspDotNetDev 25-Apr-11 19:56pm    
My recommendation (see my answer) is to serialize to / deserialize from XML.
Sergey Alexandrovich Kryukov 25-Apr-11 19:58pm    
Yes, voted 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900