How to set the Font of the ListBox in C#?
In Windows Forms, ListBox control is used to show multiple elements in a list, from which a user can select one or more elements and the elements are generally displayed in multiple columns. In ListBox, you are allowed to set the font of the content present in the ListBox using Font Property of the ListBox which makes your ListBox more attractive. You can set this property in two different ways:
1. Design-Time: It is the easiest way to set the font of the content present in the ListBox as shown in the following steps:
- Step 1: Create a windows form as shown in the below image:
Visual Studio -> File -> New -> Project -> WindowsFormApp

- Step 2: Drag the ListBox control from the ToolBox and drop it on the windows form. You are allowed to place a ListBox control anywhere on the windows form according to your need.

- Step 3: After drag and drop you will go to the properties of the ListBox control to set the font of the content present in the ListBox.

Output:

2. RunTime: It is a little bit trickier than the above method. In this method, you can set the font of the content present in the ListBox control programmatically with the help of given syntax:
public override System.Drawing.Font Font { get; set; }
Here, Font indicates the font applied on the content displayed by the ListBox. The following steps show how to set the font of the content present in the ListBox dynamically:
- Step 1: Create a list box using the ListBox() constructor is provided by the ListBox class.
// Creating ListBox using ListBox class constructor ListBox mylist = new ListBox();
- Step 2: After creating ListBox, set the Font property of the ListBox provided by the ListBox class.
// Setting the font of the ListBox mylist.Font = new Font("Bradley Hand ITC", 12); - Step 3: And last add this ListBox control to the form using Add() method.
// Add this ListBox to the form this.Controls.Add(mylist);
Example:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceWindowsFormsApp26 {publicpartialclassForm1 : Form {publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender, EventArgs e){// Creating and setting the// properties of ListBoxListBox mylist =newListBox();mylist.Location =newPoint(287, 109);mylist.Size =newSize(120, 95);mylist.Font =newFont("Bradley Hand ITC", 12);mylist.Items.Add("Geeks");mylist.Items.Add("GFG");mylist.Items.Add("GeeksForGeeks");mylist.Items.Add("gfg");// Adding ListBox control to the formthis.Controls.Add(mylist);}}}chevron_rightfilter_noneOutput:

Recommended Posts:
- How to set the Font of the Button in C#?
- How to set the Font of the Content present in the RichTextBox in C#?
- How to set the font of the TextBox Content in C#?
- How to set the font of the CheckBox in C#?
- How to set the font of the content present in the ComboBox in C#?
- How to set the Font of the Content Present in the Label in C#?
- How to set the Font of the RadioButton in C#?
- How to set the Background Color of a ListBox in C#?
- How to set the Foreground Color of a ListBox in C#?
- How to set the Location of the ListBox in C#?
- How to set the Size of the ListBox in C#?
- How to style the Border of ListBox in C#?
- How to add Items in ListBox in C#?
- How to sort elements in the ListBox in C#?
- How to set the Visibility of ListBox in C#?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


