In Windows Forms, the 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. The ListBox class in C# is used to represent the Windows list box and also provides different types of properties, methods, and events. It is defined under System.Windows.Forms namespace.
Collections in ListBox
The ListBox class contains three different types of collection classes, which are listed below:
- ListBox.ObjectCollection: This class holds all the elements contained in the ListBox control.
- ListBox.SelectedObjectCollection: This class holds a collection of the selected items, which is a subset of the items contained in the ListBox control.
- ListBox.SelectedIndexCollection: This class holds a collection of the selected indexes, which is a subset of the indexes of the ListBox.ObjectCollection and these indexes specify elements that are selected.
There are two ways to create a ListBox in Windows forms which are mentioned below.
- Drag and drop (Design-Time)
- Custom ListBox (Run-Time)
1. Drag and drop (Design-Time)
This is the easiest way to create a ListBox in Windows Forms using Visual Studio we just have to open the toolbox and drag and drop the ListBox on the form in the designer and further we can change the appearance of the ListBox using the properties. Follow these steps to create a ListBox.
Step 1: Now locate the project with the name here we are using the default name which is Form1 and it will open a form in the editor that we can further modify.

In the image, we have two files that are open one Design and there is Form1.cs these two play a major role. We use the Form 1.cs file for the custom logic.
Step 2: Now open a Toolbox go to the view > Toolbox or ctrl + alt + x.

Step 3. Now open the common controls and drag and drop the ListBox on the form where we want it to be placed.
Step 4. Now open the properties of the button right-click on the ListBox and it will open the properties solution explorer now we can change the appearance and the behaviour of the button.
Step 5: In the properties, we can make different types of changes we can add the items in the list which is available at the left-most bottom or we can click on Collection(Item) in the properties as shown in the image.
It will open the new box (String Collection Editor) here we can add different list items and each line is a separate list as shown in the image after adding the list click on the OK button.
Output:
2. Custom ListBox (Run Time)
In this method, we are going to modify the Form1.cs file and add custom code modification in C# with the help of the ListBox class. The following steps show how to create a ListBox dynamically:
Step 1: Create a ListBox control using the ListBox() constructor is provided by the ListBox class.
// Creating a ListBox control
ListBox mylist = new ListBox();
Step 2: After creating the ListBox control, set the property of the ListBox control provided by the ListBox class.
ListBox mylist = new ListBox();
mylist.Location = new Point(287, 109);
mylist.Size = new Size(120, 95);
mylist.ForeColor = Color.Purple;
mylist.Items.Add(123);
mylist.Items.Add(456);
mylist.Items.Add(789);
Step 3: And lastly add this ListBox control to the form using the following statement:
// Adding ListBox control
// to the form
this.Controls.Add(mylist);
Step 4: Double-click on the form in Design view. It will open the Form1.cs file where code is written in C#. Here. the program file is Form 1.cs Now write this code in Form1.cs file
Form1.cs file:
C#
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Creating and setting the
// properties of ListBox
ListBox mylist = new ListBox();
mylist.Location = new Point(287, 109);
mylist.Size = new Size(120, 95);
mylist.ForeColor = Color.Purple;
mylist.Items.Add("C#");
mylist.Items.Add("Java");
mylist.Items.Add("C");
mylist.Items.Add("C++");
// Adding ListBox control
// to the form
this.Controls.Add(mylist);
}
}
}
Output:
Enabling Multi-Selection in ListBox
By default, the ListBox allows selecting only one item at a time. If we allow users to select multiple items then we need to set the SelectionMode property.
SelectionMode options:
The SelectionMode options are listed below:
- SelectionMode.One: It is a default one, in this only one item can be selected.
- SelectionMode.MultiSimple: In this multiple items can be selected by clicking them.
- SelectionMode.MultiExtended: In this multiple items can be selected with Shift or Ctrl keys.
Example:
C#
private void Form1_Load(object sender, EventArgs e)
{
ListBox myList = new ListBox
{
Location = new Point(287, 109),
Size = new Size(120, 95),
ForeColor = Color.DarkBlue,
// Enable multi-selection
SelectionMode = SelectionMode.MultiExtended
};
myList.Items.Add("Python");
myList.Items.Add("JavaScript");
myList.Items.Add("Go");
myList.Items.Add("Ruby");
// Handle selected index changed event
myList.SelectedIndexChanged += (s, ev) =>
{
var selectedItems = ((ListBox)s).SelectedItems;
string message = "Selected items:\n";
foreach (var item in selectedItems)
{
message += item + "\n";
}
MessageBox.Show(message, "Selection");
};
this.Controls.Add(myList);
}
Explanation: Here, we are creating a ListBox where users can select multiple items and shows a message box listing their selections whenever the selection changes.
Event Handling in ListBox
The ListBox can do anything whenever the user interacts with it.
- SelectedIndexChanged: This happens when the user picks a different item in the list.
- MouseDoubleClick: This happens when the user double-clicks on the list.
- KeyDown and KeyUp: These happen when the user presses or releases a key while the list is active.
Note: These events help the program to respond what the user does with the ListBox.
C#
// Handling selection change to update
// other UI elements or perform actions.
myList.SelectedIndexChanged += (sender, e) =>
{
ListBox list = (ListBox)sender;
if (list.SelectedItem != null)
{
MessageBox.Show("You selected: " + list.SelectedItem.ToString());
}
};
Constructor
ListBox(): This constructor is used to initialize a new instance of the ListBox class.
Properties
The properties of the list box class is listed below:
This property is not commonly used in ListBox as it is primarily item-based.
Properties | Description |
---|
AutoSize | This property is used to get or set a value that indicates whether the control resizes based on its contents. |
---|
BackColor | This property is used to get or set the background colour for the control. |
---|
BorderStyle | This property indicates the border style for the control. |
---|
Font | This property is used to get or set the font of the text displayed by the control. |
---|
ForeColor | This property is used to get or set the foreground colour of the control. |
---|
Height | This property is used to get or set the height of the control. |
---|
Location | This property is used to get or set the coordinates of the upper-left corner of the ListBox control relative to the upper-left corner of its form. |
---|
Name | This property is used to get or set the name of the control. |
---|
TabStop | This property is used to get or set a value that shows whether the user can press the TAB key to provide the focus to the ListBox. |
---|
Size | This property is used to get or set the height and width of the control. |
---|
Text | This property is used to get or set the text to be displayed in the RichTextBox control. |
---|
Visible | This property is used to get or set a value indicating whether the control and all its child controls are displayed. |
---|
Width | This property is used to get or set the width of the control. |
---|
ColumnWidth | This property is used to get or set the width of columns in a multicolumn ListBox. |
---|
HorizontalExtent | This property is used to get or set the width by which the horizontal scroll bar of a ListBox can scroll. |
---|
ItemHeight | This property is used to get or set the height of an item in the ListBox. |
---|
Items | This property is used to get the items of the ListBox. |
---|
PreferredHeight | This property is used to get the combined height of all items in the ListBox. |
---|
SelectedIndex | This property is used to get or set the zero-based index of the currently selected item in a ListBox. |
---|
SelectedItem | This property is used to get or set the currently selected item in the ListBox. |
---|
SelectedIndices | This property is used to get a collection that contains the zero-based indexes of all currently selected items in the ListBox. |
---|
Sorted | This property is used to get or set a value indicating whether the items in the ListBox are sorted alphabetically. |
---|
TopIndex | This property is used to get or set the index of the first visible item in the ListBox. |
---|
Similar Reads
C# | ListBox Class
In Windows Forms, the 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. The ListBox class in C# is used to represent the Windows list box and also provides different types of p
7 min read
C# List Class
In C#, the List<T> class represents the list of objects that can be accessed by index. It comes under the System.Collections.Generic namespace. List class can be used to create a collection of different types like integers, strings, etc. List<T> class also provides the methods to search,
7 min read
C# | RichTextBox Class
In C#, the RichTextBox control provides rich text editing controls, advanced formatting features, and loading rich text format (RTF) files. In other words, RichTextBox controls allow displaying or editing flow content, including paragraphs, images, tables, etc. The RichTextBox class in C# is used to
6 min read
C# LinkedList Class
LinkedList<T> class in C# is the part of the removal namespace. This generic type allows fast inserting and removal of elements. It implements a classic linked list. Each object is separately allocated. In the LinkedList, certain operations do not require the whole collection to be copied. But
5 min read
C# Stack Class
In C#, the Stack<T> class represents a Last-in-First-out (LIFO) collection of objects. The stack is the part of the System.Collections.Generic namespace. This class allows us to push elements onto the stack, pop elements from the stack, and peek at the top element without removing it.The capac
5 min read
C# | GroupBox Class
In Windows Forms, GroupBox is a container that contains multiple controls, and the controls are related to each other. In other words, GroupBox is a frame display around a group of controls with a suitable optional title. A GroupBox in C# is used to categorize the related controls in a group. The Gr
7 min read
C# ListDictionary Class
In C#, the ListDictionary class is the part of the System.Collections.Specialized namespace. It is a collection that stores key-value pairs. It is used in scenarios where the number of elements is relatively small and the order of insertion needs to be preserved.It uses a single LinkedList to store
4 min read
C# | Collection Class
.math-table { border-collapse: collapse; width: 100%; } .math-table td { border: 1px solid #5fb962; text-align: left !important; padding: 8px; } .math-table th { border: 1px solid #5fb962; padding: 8px; } .math-table tr>th{ background-color: #c6ebd9; vertical-align: middle; } .math-table tr:nth-chil
5 min read
C# SortedSet Class
SortedSet class in C# represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace.In C#, the SortedSet class can be used to store, remove, or view elements.It maintains ascending order and does not store duplicate elements.It is suggested to
5 min read
C# - FileInfo Class Methods
In this article, we will explain the FileInfo class, its methods, properties, etc. The System.IO namespace is one of the most important namespaces we used while we are working with files in C#. FileInfo Class:It does not have static methods and it can only be used on instantiated objects. The class
3 min read