How to Use Listbox in Visual Studio
A ListBox control can be very effective in your Visual Studio application when you need the user to select one or multiple items from a list. A ListBox control is available for multiple computer programming languages in Visual Studio, such as Visual Basic. The control can be used to allow the user to select only one item by specifying the selection behavior to "One." Set the behavior to "MultiSimple" to allow the user to select multiple items.
Instructions
-
-
1
Launch Microsoft Visual Basic Express and click "New Project" from the left pane of your screen to launch the New Project dialog window. Click "Windows Forms Application" and click "OK" to start a new project.
-
2
Double-click "ListBox" from the Toolbox pane to add a new list box control to the form. Double-click "Button" to add a new button.
-
-
3
Double-click the form to create a form load event. Add the following code to add five new items to the ListBox control:
Me.ListBox1.Items.Add("list box item 1")
Me.ListBox1.Items.Add("list box item 2")
Me.ListBox1.Items.Add("list box item 3")
Me.ListBox1.Items.Add("list box item 4")
Me.ListBox1.Items.Add("list box item 5")
-
4
Click "Form1.vb [Design]" to switch back to form view and double-click "Button1" to create a click event. Copy and paste the following code to loop through each of the selected items and display them using a message box:
For Each item In Me.ListBox1.SelectedItems
MessageBox.Show(item.ToString)
Next item
-
5
Press "F5" to run the program and click one item on the ListBox control. Click "Button1" to display the message box. Close the form by clicking the "X" located in the upper-right corner of the form window.
-
6
Right-click the ListBox control and click "Properties." Choose "MultiSimple" next to "SelectionMode" and press "F5" to run the program.
-
7
Click more than one item on the ListBox control and click "Button1" to display a message box for each item selected.
-
1
References
- Photo Credit Stockbyte/Stockbyte/Getty Images