C# Windows Forms Font Examples

Learn about Fonts in C# Windows Forms Controls via simple examples in this

Gets or sets the font of the text displayed by the control.

public virtual System.Drawing.Font Font { get; set; }

The Font property is the font to apply to the text displayed by the control. The default is the value of the DefaultFont property.

The Font property is an ambient property. An ambient property is a control property that, if not set, is retrieved from the parent control. For example, a Button will have the same BackColor as its parent Form by default.

Example 1: Set Font from FontDialog

The following code example displays a FontDialog to the user and changes the Font of a DateTimePicker control. This example requires that you have a Form with Button and a DateTi`mePicker on it:

private void myButton_Click(object sender, EventArgs e)
{
   FontDialog myFontDialog = new FontDialog();
   if(myFontDialog.ShowDialog() == DialogResult.OK)
   {
      // Set the control's font.
      myDateTimePicker.Font = myFontDialog.Font;
   }
}

Example 2: Adjusting Font

Because the Font is immutable (meaning that you cannot adjust any of its properties), you can only assign the Font property a new Font. However, you can base the new font on the existing font.

The following is an example of how to adjust the existing font to make it bold:

myControl.Font = new Font(myControl.Font,   
    myControl.Font.Style | FontStyle.Bold);

FontChanged Event

The Control.FontChanged Event Occurs when the Font property value changes.

public event EventHandler FontChanged;

Example 1: FontChanged Event

The following code example demonstrates the FontChanged event.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
{
    private RadioButton radioButton1;
    private RadioButton radioButton2;

    public Form1()
    {
        InitializeComponent();
    }
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    private void InitializeComponent()
    {
        this.radioButton1 = new System.Windows.Forms.RadioButton();
        this.radioButton2 = new System.Windows.Forms.RadioButton();
        this.SuspendLayout();
        // 
        // radioButton1
        // 
        this.radioButton1.AutoSize = true;
        this.radioButton1.Location = new System.Drawing.Point(0, 0);
        this.radioButton1.Name = "radioButton1";
        this.radioButton1.Size = new System.Drawing.Size(62, 17);
        this.radioButton1.TabIndex = 0;
        this.radioButton1.TabStop = true;
        this.radioButton1.Text = "Button1";
        this.radioButton1.UseVisualStyleBackColor = true;
        this.radioButton1.FontChanged += new System.EventHandler(this.radioButton1_FontChanged);
        // 
        // radioButton2
        // 
        this.radioButton2.AutoSize = true;
        this.radioButton2.Location = new System.Drawing.Point(0, 39);
        this.radioButton2.Name = "radioButton2";
        this.radioButton2.Size = new System.Drawing.Size(126, 17);
        this.radioButton2.TabIndex = 1;
        this.radioButton2.TabStop = true;
        this.radioButton2.Text = "Change Button1 font.";
        this.radioButton2.UseVisualStyleBackColor = true;
        this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
        // 
        // Form1
        // 
        this.ClientSize = new System.Drawing.Size(292, 273);
        this.Controls.Add(this.radioButton2);
        this.Controls.Add(this.radioButton1);
        this.Name = "Form1";
        this.ResumeLayout(false);
        this.PerformLayout();
    }

    private void radioButton2_CheckedChanged(object sender, EventArgs e)
    {
        radioButton1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    }

    private void radioButton1_FontChanged(object sender, EventArgs e)
    {
        MessageBox.Show("The font has been changed.");
    }
}

Example 2: Second FontChanged Event

This example will comprise the following files:

  • FontChangedEvent.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). FontChangedEvent.cs

Create a file named FontChangedEvent.cs

Here is the full code

using System;
using System.Windows.Forms;

namespace FolderBrowserDialogExample {
  class Form1 : Form {
    // The main entry point for the application.
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }

    public Form1() {
      InitializeComponent();
    }

    private void InitializeComponent() {
      SuspendLayout();
      //
      // radioButton1
      //
      radioButton1.Location = new System.Drawing.Point(0, 0);
      radioButton1.Name = "radioButton1";
      radioButton1.Size = new System.Drawing.Size(150, 17);
      radioButton1.TabStop = true;
      radioButton1.Text = "Button1";
      radioButton1.FontChanged += new System.EventHandler(radioButton1_FontChanged);
      //
      // radioButton2
      //
      radioButton2.Location = new System.Drawing.Point(0, 39);
      radioButton2.Name = "radioButton2";
      radioButton2.Size = new System.Drawing.Size(150, 17);
      radioButton2.TabStop = true;
      radioButton2.Text = "Change Button1 font.";
      radioButton2.CheckedChanged += new System.EventHandler(radioButton2_CheckedChanged);
      //
      // Form1
      //
      ClientSize = new System.Drawing.Size(292, 273);
      Controls.Add(radioButton2);
      Controls.Add(radioButton1);
      Name = "Form1";
      ResumeLayout(false);
      PerformLayout();
    }

    private void radioButton2_CheckedChanged(object sender, EventArgs e) {
      radioButton1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.0f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
    }

    private void radioButton1_FontChanged(object sender, EventArgs e) {
      MessageBox.Show("The font has been changed.");
    }

    private RadioButton radioButton1 = new RadioButton();
    private RadioButton radioButton2 = new RadioButton();
  }
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Control.OnFontChanged(EventArgs) Method

This Raises the FontChanged event.

protected virtual void OnFontChanged (EventArgs e);

Example

The following code example is an event-raising method that is executed when the Text property value changes. The Control class has several methods with the name pattern OnPropertyNameChanged that raise the corresponding PropertyNameChanged event when the PropertyName value changes (PropertyName represents the name of the corresponding property).

The following code example changes the ForeColor of a TextBox derived class displaying currency data. The example converts the text to a decimal number and changes the ForeColor to Color.Red if the number is negative and to Color.Black if the number is positive. This example requires that you have a class that derives from the TextBox class.

protected override void OnTextChanged(System.EventArgs e)
{
   try
   {
      // Convert the text to a Double and determine
      // if it is a negative number.
      if(double.Parse(this.Text) < 0)
      {
         // If the number is negative, display it in Red.
         this.ForeColor = Color.Red;
      }
      else
      {
         // If the number is not negative, display it in Black.
         this.ForeColor = Color.Black;
      }
   }
   catch
   {
      // If there is an error, display the 
      // text using the system colors.
      this.ForeColor = SystemColors.ControlText;
   }

   base.OnTextChanged(e);
}

Raising an event invokes the event handler through a delegate. The OnFontChanged method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

DefaultFont

Gets the default font of the control.

public static System.Drawing.Font DefaultFont { get; }

The property value is the Font, the default Font of the control. The value returned will vary depending on the user's operating system the local culture setting of their system.

Example

The following code example demonstrates how to use the DefaultBackColor, DefaultFont, and DefaultForeColor members. To run the example, paste the following code in a form containing a ListBox called ListBox1. Call the Populate_ListBox method in the form's constructor or Load event-handling method.

// The following method displays the default font, 
// background color and foreground color values for the ListBox  
// control. The values are displayed in the ListBox, itself.

private void Populate_ListBox()
{
    ListBox1.Dock = DockStyle.Bottom;

    // Display the values in the read-only properties 
    // DefaultBackColor, DefaultFont, DefaultForecolor.
    ListBox1.Items.Add("Default BackColor: " + 
        ListBox.DefaultBackColor.ToString());
    ListBox1.Items.Add("Default Font: " + 
        ListBox.DefaultFont.ToString());
    ListBox1.Items.Add("Default ForeColor:" + 
        ListBox.DefaultForeColor.ToString());
}

Resetting Font

You can use the Control.ResetFont to reset the Font property to its default value.

public virtual void ResetFont ();