ListBox control is used to display the list of items and allows to sort the items. One interesting thing is, it displays the items in multiple columns also. For example, Windows Explorer displays the list of files (say Tiles view); in multiple columns. Depending on your requirement you can select whether to display the items one by one or in multi columns.
Another interesting thing is, it allows to select the list items; one or multiple items at a time.
It has multiple properties and we discuss few of them, commonly used properties, in this Article.
ListBox control main purpose is to display the list of items. And allows to sort the items. Items property is used to holds the list of items; it is a collection and when we add the items to it, the items will be displayed in the ListBox control.
Adding list items to ListBox control
We can not add items directly to the ListBox; instead it allows to add to it’s Items collection. ListBox maintains a collection to keep it’s items. As mentioned above, it has a property Items to retrieve the items collection. Items property is of ObjectCollection type. It has the Add() method, to add an item to the collection. So, we use this method to add items to the ListBox control.
As Items property is the collection, we can also add the entries to it, by using index value; like an Array. But, we have to be cautious with the index value; if it is out of range, C# throws ArgumentOutOfRangeException exception.
listBox.Items = "Rocket";
Removing items from ListBox control
It has Remove() method, which allows to remove an item from it’s Items collection. It’s simple right? Yes. If it has multiple items, how it works? It removes the one which occurred first.
When we call Remove() method, to remove any item from ListBox; it removes the item; and then the indexes of subsequent items in the ListBox also changed. So, we have to be cautious with the index values, if we saved them before we remove an item from the ListBox; those saved index values are invalid as these are already updated with the new index values when we remove an item from the ListBox.
If we know the index of the item to remove, we can use RemoveAt() method to remove the item from particular index location. Below code removes the first item from the list;
We can also add or remove the items to the ListBox during the design in Design window (Visual Studio IDE).
Sorting of ListBox items
As mentioned above, this control supports sorting. Sorted property is used for this. When set this property to true, all the items in the ListBox control will be sorted in alphabetic order. Always it sorts the items in ascending order; if you want to change the order, you can provide sorted items to the ListBox and set this property to false.
When we use DataSource property to provide the data to this control; Sorted property should be set to false. And the data source should provide sorted list of items. May be, we will discuss this in a separate article on DataSoruce property. When we use this DataSoruce property, we are not allowed to modify Items collection, as the data is managing through DataSource property.
Selection mode in ListBox control
When items are populated in ListBox control, we can select a single item or multiple items at a time. This can be controlled using SelectionMode property. When we set the value One, means we can select single item at a time. MultiSimple or MultiExtended values, we use for multi-items selection. If no selection allowed, we set the value None to this property.
We can access selected items from SelectedItem or SelectedItems properties. SelectedItems is a collection, and it holds all the selected items when multi-item selection mode is set. When the selection mode is set to One, it has a single item in it. SelectedIndex or SelectedIndices properties are also used to holds the index values of the selected items.
As mentioned above, we have to be cautious when using these properties; when removing items from the control.
Events in ListBox control
ListBox control triggers events when needed. When the events are mapped to event handlers, these handlers will be executed when the events triggered. Most commonly used events are SelectedIndexChanged event. This will be triggered when the value of SelectedIndex or SelectedIndices collection has changed.
When you remove an item from the ListBox, it triggers this event. We have to be cautious when we remove items from the ListBox control as the item indices will change.
Here is the code to safely remove items from ListBox;
while (listBox.SelectedItems.Count > 0)
Complete working code
Here is the complete working example. “Add” button is to add the item to the ListBox; sort button to, sort items; remove button to remove selected items. Also added buttons to switch between single & multi selection; single & multi column display.
Screenshot of our working program looks like below;
We will discuss more topics, as we go.
An alternate toListBox control isCheckedListBox control; which displays check boxes for each item in the list to allow the user to select the option.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.