FontConverter Class
Assembly: System.Drawing (in system.drawing.dll)
A type converter is used to convert values between data types. A type converter also supports property configuration at design time by providing text-to-value conversion or a list of values that users can select from. Access the FontConverter class through the TypeDescriptor class by calling the GetConverter method.
The following code example demonstrates how to use the FontConverter to convert a Font to and from a string. This example is designed to be used with Windows Forms. Paste this code into a form and call the ShowFontStringConversion method when handling the form's Paint event, passing e as PaintEventArgs.
Private Sub ShowFontStringConversion(ByVal e As PaintEventArgs) ' Create the FontConverter. Dim converter As System.ComponentModel.TypeConverter = _ System.ComponentModel.TypeDescriptor.GetConverter(GetType(Font)) Dim font1 As Font = _ CType(converter.ConvertFromString("Arial, 12pt"), Font) Dim fontName1 As String = _ converter.ConvertToInvariantString(font1) Dim fontName2 As String = converter.ConvertToString(font1) e.Graphics.DrawString(fontName1, font1, Brushes.Red, 10, 10) e.Graphics.DrawString(fontName2, font1, Brushes.Blue, 10, 30) End Sub
private void ShowFontStringConversion(PaintEventArgs e)
{
// Create the FontConverter.
System.ComponentModel.TypeConverter converter =
System.ComponentModel.TypeDescriptor.GetConverter(
get_Font().GetType());
Font font1 = (Font)(converter.ConvertFromString("Arial, 12pt"));
String fontName1 = converter.ConvertToInvariantString(font1);
String fontName2 = converter.ConvertToString(font1);
e.get_Graphics().DrawString(fontName1, font1, Brushes.get_Red(), 10,
10);
e.get_Graphics().DrawString(fontName2, font1, Brushes.get_Blue(), 10,
30);
} //ShowFontStringConversion
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.