Getting Started with Windows Forms FontListBox

18 Nov 20189 minutes to read

This section explains how to get started with the Windows Forms FontListBox control and its basic features.

Assembly Deployment

Refer to the Control Dependencies section for the list of assemblies and NuGet packages required to use the FontListBox control.

Refer to NuGet Packages to learn how to install NuGet packages in a Windows Forms application.

To install the required NuGet package using the NuGet Package Manager Console, run the following command:

Install-Package Syncfusion.Tools.Windows

Adding FontListBox via Designer

The FontListBox control can be added using the following steps:

  1. Create a new Windows Forms application in Visual Studio.

  2. Drag the FontListBox control from the Toolbox and drop it onto the Windows Form designer. The following dependent assemblies are added automatically:

    • Syncfusion.Grid.Base
    • Syncfusion.Grid.Windows
    • Syncfusion.Shared.Base
    • Syncfusion.Shared.Windows
    • Syncfusion.Tools.Base
    • Syncfusion.Tools.Windows

Drag and drop FontListBox from toolbox in WindowsForms

Adding FontListBox via Code

The following steps describe how to create a FontListBox control programmatically:

  1. Create a C# or VB.NET Windows Forms application in Visual Studio.

  2. Add the required assembly references to the project.

    • Syncfusion.Grid.Base
    • Syncfusion.Grid.Windows
    • Syncfusion.Shared.Base
    • Syncfusion.Shared.Windows
    • Syncfusion.Tools.Base
    • Syncfusion.Tools.Windows
  3. Include the required namespace.

    using Syncfusion.Windows.Forms.Tools;
    Imports Syncfusion.Windows.Forms.Tools
  4. Create an instance of the FontListBox control, set its size, and add it to the Form1 form.

    public partial class Form1 : Form
    {
        private FontListBox fontListBox1;
        public Form1()
        {
            InitializeComponent();
            fontListBox1 = new FontListBox();
            fontListBox1.Size = new Size(160, 94);
            fontListBox1.Location = new Point(20, 20);
            this.Controls.Add(fontListBox1);
        }
    }
    Public Partial Class Form1
        Inherits Form
       
        Private fontListBox1 As FontListBox
        Public Sub New()
            InitializeComponent()
            fontListBox1 = New FontListBox()
            fontListBox1.Size = New Size(160, 94)
            fontListBox1.Location = New Point(20, 20)
            Me.Controls.Add(fontListBox1)
        End Sub
    End Class

Adding FontListBox control via code in WindowsForms

Selecting Items

At run time, users can select items from the FontListBox control. The text style is updated based on the selected font item. The following example uses a Label control to preview the selected font.

private Label label;

public Form1()
{
    InitializeComponent();

    // Hook the Load event to Form1_Load.
    this.Load += new System.EventHandler(this.Form1_Load);
}

private void Form1_Load(object sender, System.EventArgs e)
{
    // Create the Label control.
    label = new Label();
    label.Anchor = System.Windows.Forms.AnchorStyles.None;
    label.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
    label.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F);
    label.Location = new System.Drawing.Point(220, 30);
    label.Size = new System.Drawing.Size(360, 95);
    label.Text = "The FontListBox is a list box derived control that is automatically populated with the fonts installed on the user's system. It provides an easy way to fill a list box with system fonts.";

    // Add the control.
    this.Controls.Add(label);

    // Wire up the selection event for the control.
    fontListBox1.SelectedIndexChanged += new System.EventHandler(this.FontListBox1_SelectedIndexChanged);
}

// Selection event for FontListBox.
private void FontListBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    if (this.fontListBox1.SelectedItem != null)
    {
        this.label.Font = new System.Drawing.Font(this.fontListBox1.SelectedItem.ToString(), 10, System.Drawing.FontStyle.Regular);
    }
}
Public Sub New()
    InitializeComponent()

    ' Hook the Load event to Form1_Load.
    AddHandler Me.Load, AddressOf Form1_Load
End Sub

Friend label As Label

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    ' Create the Label control.
    label = New Label()
    label.Anchor = System.Windows.Forms.AnchorStyles.None
    label.FlatStyle = System.Windows.Forms.FlatStyle.Flat
    label.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75F)
    label.Location = New System.Drawing.Point(220, 30)
    label.Size = New System.Drawing.Size(360, 95)
    label.Text = "The FontListBox is a list box derived control that is automatically populated with the fonts installed on the user's system. It provides an easy way to fill a list box with system fonts."

    ' Add the control.
    Me.Controls.Add(label)

    ' Wire up the selection event for the control.
    AddHandler fontListBox1.SelectedIndexChanged, AddressOf FontListBox1_SelectedIndexChanged
End Sub

' Selection event for FontListBox.
Private Sub FontListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    If Me.fontListBox1.SelectedItem IsNot Nothing Then
        Me.label.Font = New System.Drawing.Font(Me.fontListBox1.SelectedItem.ToString(), 10, System.Drawing.FontStyle.Regular)
    End If
End Sub

WindowsForms FontListBox showing change the text style

Selection Mode

The items in the FontListBox control can be selected based on the value of the SelectionMode property. Depending on the selected mode, users can select one or more items by using the mouse or keyboard.

The following options are available in the FontListBox control.

SelectionMode Description
None No items can be selected.
One Only one item can be selected.
MultiSimple Multiple items can be selected.
MultiExtended Multiple items can be selected using the SHIFT, CTRL, and arrow keys to make selections.
fontListBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
fontListBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended

Windows Forms FontListBox shows different selection of font list