C# ListBox Control
The ListBox control enables you to display a list of items to the user that the user can select by clicking.
In addition to display and selection functionality, the ListBox also provides features that enable you to efficiently add items to the ListBox and to find text within the items of the list. You can use the Add or Insert method to add items to a list box. The Add method adds new items at the end of an unsorted list box.
If you want to retrieve a single selected item to a variable , you can code like this.
The SelectionMode property determines how many items in the list can be selected at a time. A ListBox control can provide single or multiple selections using the SelectionMode property . If you change the selection mode property to multiple select , then you will retrieve a collection of items from ListBox1.SelectedItems property.
The following C# program initially fill seven days in a week while in the form load event and set the selection mode property to MultiSimple. At the Button click event it will display the selected items.
How to bind a ListBox to a List ?
First you should create a fresh List Object and add items to the List.
The next step is to bind this List to the Listbox. In order to do that you should set datasource of the Listbox.
listBox1.DataSource = nList;
Full Source code
How to bind a listbox to database values ?
First you should create a connection string and fetch data from database to a Dataset.
After that you should set Listbox datasoure as Dataset.
How to refresh DataSource of a ListBox ?
How to clear the Listbox if its already binded with datasource ?
When you want to clear the Listbox, if the ListBox already binded with Datasource, you have to set the Datasource of Listbox as null.
listBox1.DataSource = null;
How to SelectedIndexChanged event in ListBox ?
This event is fired when the item selection is changed in a ListBox. You can use this event in a situation that you want select an item from your listbox and accodring to this selection you can perform other programming needs.
You can add the event handler using the Properties Window and selecting the Event icon and double-clicking on SelectedIndexChanged as you can see in following image.
The event will fire again when you select a new item. You can write your code within SelectedIndexChanged event . When you double click on ListBox the code will automatically come in you code editor like the following image.
From the following example you can understand how to fire the SelectedIndexChanged event.
First you should drag two listboxes on your Form. First listbox you should set the List as Datasource, the List contents follows:
When you load this form you can see the listbox is populated with List and displayed first quarter and second quarter. When you click the "Fist Quarter" the next listbox is populated with first quarter months and when you click "Second Quarter" you can see the second listbox is changed to second quarter months. From the following program you can understand how this happened.
- C# Visual Studio IDE
- How to Create a C# Windows Forms Application
- C# Label Control
- C# Button Control
- C# TextBox Control
- C# ComboBox
- C# Checked ListBox Control
- C# RadioButton Control
- C# CheckBox Control
- C# PictureBox Control
- C# ProgressBar Control
- C# ScrollBars Control
- C# DateTimePicker Control
- C# Treeview Control
- C# ListView Control
- C# Menu Control
- C# MDI Form
- C# Color Dialog Box
- C# Font Dialog Box
- C# OpenFile Dialog Box
- C# Print Dialog Box
- keyPress event in C# , KeyDown event in C# , KeyUp event in C#
- How to create Dynamic Controls in C# ?
- Keep Form on Top of All Other Windows
- C# Timer Control