- A ListBox control is used to display items in the form of a list where we can make selections.
- The ListBox control is same like ComboBox control.
- You can place a ListBox control on the form by drag and drop it from the toolbox. The ListBox control looks like as follows:
Properties of ListBox Control
Some of the commonly used properties of ListBox Control are listed below in the Table:
Methods of ListBox Control
Some of the commonly used methods of ListBox Control are listed below in the Table:
Events of ListBox Control
Some of the commonly used Events of ListBox Control are listed below in the Table:
Here is a simple example of ListBox control. To understand the use of the ListBox control follows the given steps:
- Add one ListBoxBox control to the Form by drag and drop it from the Toolbox.
- Select the ListBox and press F4 key to open its properties window and click on the ellipse (…) button besides the items option this will open a String Collection Editor. Add the following items and click on OK button to close the dialog box as given in the below diagram:
- Now double click on the list-box in the form and add the following code in its code window:
MessageBox.Show(“You selected “+ listBox1.SelectedItem.ToString());
- In the above code, ListBox1_SelectedIndexChanged method handles the SelectedIndexChanged event of the ListBox. Whenever we select an item from the list box a message displayed using the MsgBox method.
- Now run the application using F5 key. This will produce a Form at run time, Select any item from the list box a message box with selected color will be displayed as given below in the diagram: