A C# ListBox control provides a user interface to display a list of items. Users can select one or more items from the list. A ListBox may be used to display multiple columns and these columns may have images and other controls.
In this tutorial, we will learn how to create a C# ListBox control at design-time as well as at run-time. We will also see how to create a multiple-column ListBox control with single and multiple selections. This article also covers most of the properties and methods of the ListBox control.
Creating a C# ListBox
There are two approaches to create a ListBox control in Windows Forms. Either we can use the Forms designer to create a control at design-time or we can use the ListBox class to create a control at run-time.
Create a ListBox at Design-time
In our first approach, we are going to create a ListBox control at design-time using the Forms designer.
To create a ListBox control at design-time, we simply drag a ListBox control from the Toolbox and drop it to a Form in Visual Studio. After you drag and drop a ListBox onto a Form, the ListBox looks as in Figure 1. Once a ListBox is on the Form, you can move it around and resize it using the mouse and set its properties and events.
Figure 1
Create a ListBox Dynamically
The ListBox class represents a ListBox control in Windows Forms. To create a ListBox control at run-time, we create an instance of the ListBox class, set its properties and add a ListBox object to the Form controls.
The first step to create a dynamic ListBox is to create an instance of the ListBox class. The following code snippet creates a ListBox control object:
- ListBox listBox1 = new ListBox();
In the next step, you may set the properties of a ListBox control. The following code snippet sets the location, width, height, background color, foreground color, Text, Name, and Font properties of a ListBox:
- listBox1.Location = new System.Drawing.Point(12, 12);
- listBox1.Name = "ListBox1";
- listBox1.Size = new System.Drawing.Size(245, 200);
- listBox1.BackColor = System.Drawing.Color.Orange;
- listBox1.ForeColor = System.Drawing.Color.Black;
Once the ListBox control is ready with its properties, the next step is to add the ListBox to a Form. To do so, we use the Form.Controls.Add method that adds a ListBox control to the Form controls and displays it on the Form based on the location and size of the control. The following code snippet adds a ListBox control to the current Form:
- Controls.Add(listBox1);
C# ListBox Properties
The easiest way to set properties is from the Properties Window. You can open the Properties window by pressing F4 or by right-clicking on a control and selecting the "Properties" menu item. The Properties window looks as in Figure 2.
Figure 2
ListBox Name Property
The Name property represents a unique name of a ListBox control. It is used to access the control in the code. The following code snippet sets and gets the name and text of a ListBox control:
- listBox1.Name = "ListBox1";
ListBox Location, Height, Width and Size Properties
The Location property takes a Point that specifies the starting position of the ListBox on a Form. You may also use the Left and Top properties to specify the location of a control from the top-left corner of the Form. The Size property specifies the size of the control. We can also use the Width and Height properties instead of the Size property. The following code snippet sets the Location, Width, and Height properties of a ListBox control:
- listBox1.Location = new System.Drawing.Point(12, 12);
- listBox1.Size = new System.Drawing.Size(245, 200);
ListBox Font
The Font property represents the font of the text of a ListBox control. If you click on the Font property in Properties window, you will see the Font name, size and other font options. The following code snippet sets the Font property at run-time:
- listBox1.Font = new Font("Georgia", 16);
ListBox Background Color and Foreground Color
The BackColor and ForeColor properties are used to set the background and foreground colors of a ListBox respectively. If you click on these properties in the Properties window, then the Color Dialog pops up.
Alternatively, you can set background and foreground colors at run-time. The following code snippet sets the BackColor and ForeColor properties:
- listBox1.BackColor = System.Drawing.Color.Orange;
- listBox1.ForeColor = System.Drawing.Color.Black;
The new ListBox with background and foreground looks as in Figure 3.
Figure 3
You can also set the borders style of a ListBox by using the BorderStyle property. The BorderStyle property is represented by a BorderStyle enumeration that has three values: FixedSingle, Fixed3D, and None.The default value of the border style is Fixed3D. The following code snippet sets the border style of a ListBox to FixedSingle:
- listBox1.BorderStyle = BorderStyle.FixedSingle;
ListBox Items
The Items property is used to add and work with items in a ListBox. We can add items to a ListBox at design-time from the Properties Window by clicking on the Items Collection as you can see in Figure 4.
Figure 4
When you click on the Collections, the String Collection Editor window will pop up where you can type strings. Each line added to this collection will become a ListBox item. I add four items as you can see from Figure 5.
Figure 5
The ListBox looks as in Figure 6.
Figure 6
You can add the same items at run-time by using the following code snippet:
- listBox1.Items.Add("Mahesh Chand");
- listBox1.Items.Add("Mike Gold");
- listBox1.Items.Add("Praveen Kumar");
- listBox1.Items.Add("Raj Beniwal");

Sydngsoft NguyenPosted Apr 19, 2022, 8:47 AM
I have a Listbox, but if I have 16 items , it will in a one column, but when it has 17 or more , it will display 2 column . How can I resolve it. Thks
Rajeesh MenothPosted Jul 29, 2015, 1:01 AM
Good One Sir..
Sabari PrabuPosted Apr 21, 2015, 7:48 AM
I need to give a folder as input to the listbox and retrive the files in that folder to the listbox with some filterations. Can anyone help me with the code???
Mahesh ChandPosted Jun 13, 2014, 12:28 AM
Code formatted
Vithal WadjePosted Jan 24, 2013, 6:25 AM
super
umair hafeezPosted Nov 24, 2012, 4:15 PM
i have a items in list box with prices. How do i add item total from the list box. in visual C#. please let me know.
umair hafeezPosted Nov 24, 2012, 4:15 PM
i have a items in list box with prices. How do i add item total from the list box. in visual C#. please let me know.
Naveen VPosted Jun 13, 2012, 12:29 PM
Mahesh, this is nice. I would like to know how we can create dependent listboxes using c#. I have a sql database where i am pulling the data. I am populating data to the first listbox(suppliername) and when i click on a supplier name i need to see the groups associated with that supplier in the secon listbox. if you can tell me how to do this that would be nice.
govidha rajaPosted Jun 5, 2012, 3:44 AM
DEAR SIR, U R DOING WONDERFUL JOB,REALY I GOT SOMEWAHT KNOWLEDGE IN LANGUAGE C++,VB.BY READING UR ARTICLE I CAN LEARN PROPERLY THE VISUAL C# USING VERY NICE TECHNICAL WORDS AND THE WAY U WRITING THE SAMPLE PROGRAMES,ONE MORE THING IS THERE DIFFERENCE ING ULTIMATE AND EXPRESS ?
Sabraz Nawaz SamsudeenPosted Apr 12, 2011, 4:19 AM
Dear Mr. Mahesh, The service that you have rendered in this site with your amazingly composed articles cannot be compared with any others' works on the net, to be honest. From novice to advance, you have drawn a track to set foot on the way to C#... definitely deserve a salute sir. S. Sabraz Nawaz, Lecturer in MIT South Eastern University of Sri Lanka. [email protected]
Vishnu GPosted Feb 4, 2011, 2:39 AM
hi, i am vishnu. i am an amatuer. please provide me help. how to assign items value for each item and retrieve item with that value
Manikavelu VelayuthamPosted Aug 31, 2010, 8:16 AM
Excellent article about List box. Top to bottom of Listbox is discussed here.