Home
C#.WinForms
Font Examples
This page was last reviewed on Mar 24, 2023.
Dot Net Perls
Font. This C# type represents a typeface. We create an instance of the Font type with its constructor. The System.Drawing namespace is required.
Shows a font
Some notes. We use the reference returned by the Font constructor to specify typefaces in Windows Forms programs. We can use Font anywhere we have System.Drawing.
FontDialog
Constructor. This example is a Windows Forms program and its uses the simplest Font constructor. We use the Font constructor in Form1_Load.
Info We specify a font name ("Times New Roman") as the first argument. The second argument is of type float and specifies the size.
Tip If you get an error about the number format of the second argument, use the "f" suffix to specify that the number should be a float.
Shows a font
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Font font = new Font("Times New Roman", 12.0f); // Set Font property and then add a new Label. this.Font = font; this.Controls.Add(new Label() { Text = "Dot Net Perls" }); this.Size = new Size(300, 200); } } }
FontStyle, FontFamily. FontFamily describes a specific family of fonts, such as the "Times New Roman" family. No style, size, or other information is part of the FontFamily.
Info In the Font constructor, we specify the FontStyle using the bitwise OR operator. This is used on enums with the Flags attribute.
Result The program renders a Label with "Times New Roman" that is bold, italic, and underlined at 16 points.
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { FontFamily family = new FontFamily("Times New Roman"); Font font = new Font(family, 16.0f, FontStyle.Bold | FontStyle.Italic | FontStyle.Underline); // Set Font property. this.Font = font; this.Controls.Add(new Label() { Text = "Dot Net Perls", Width = 250 }); this.Size = new Size(300, 200); } } }
Font exists. Here we develop code that chooses the best font for a program. Windows Forms will not create a font that does not exist—we can test for this condition.
Note We call SetFontFinal method before InitializeComponent. The font is "Cambria," which is not always present.
InitializeComponent
Info After we make the new Font object, we can test the Name property on it. If the name "Cambria" is still set, the font exists.
But If the Name property is not the same as specified, the font doesn't exist. In this case we use another font.
public partial class Form1 : Form { public Form1() { SetFontFinal(); InitializeComponent(); } private void SetFontFinal() { string fontName = "Cambria"; Font testFont = new Font(fontName, 16.0f, FontStyle.Regular, GraphicsUnit.Pixel); if (testFont.Name == fontName) { // The font exists, so use it. this.Font = testFont; } else { // The font we tested doesn't exist, so fallback to Times. this.Font = new Font("Times New Roman", 16.0f, FontStyle.Regular, GraphicsUnit.Pixel); } } }
Bold, italic, underline. The Font type also provides the properties Bold, Italic, and Underline. These properties are read-only. You can access them to determine the styles set on the Font.
Font.Style. The Style property will tell you what styles are set on the Font instance, but you cannot mutate them. Instead, you can create a new Font with the constructor.
FontFamily. FontFamily represents families of fonts separately from their styles. Suppose you use the font "Times New Roman" in several different sizes and styles in your program.
And With FontFamily, you only need one FontFamily instance for all of them. This can reduce bugs caused by string usage.
Summary. The Font type is useful in graphical programs. It cannot represent colors of fonts, but it does store information about styles, sizes and typefaces.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Mar 24, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.