ListBox Control in VB.net – Properties, Methods and Events

What is ListBox Control in VB.net?

A ListBox Control in VB.net is used to display a list of items in Windows form. It allows the user to select one or more items from the ListBox Control. Furthermore, we can add or design the list box by using the properties, methods and events window at runtime.

See also all the Basic Control Using VB.net with Source Code….

In order for you to test your VB.net Code provided in this lesson, you must test the code in your code editor. But if you wish to run this code online, we also have an Online Compiler in VB.net for you to test your VB.net Code for free.

We have learned how to use the ComboBox Control in VB.net in the previous lesson. In this lesson we shall learn How To Write a Program for ListBox Control in VB.net.

Let’s create a list box by dragging a ListBox Control from the Toolbox and dropping it on the form.

ListBox Control in VB.net Tutorial
ListBox Control in VB.net Tutorial

You can populate the list box items either from the properties window or at runtime. To add items to a ListBox, select the ListBox control and get to the properties window, for the properties of this control. Click the ellipses (…) button next to the Items property. This opens the String Collection Editor dialog box, where you can enter the values one at a line.

Properties of the ListBox Control in VB.net

The following are some of the commonly used Properties of the ListBox Control in VB.net.

#Properties of the ListBox Control in VB.netDescription
1.AllowSelectionGets a value indicating whether the ListBox currently enables selection of list items.
2.BorderStyleGets or sets the type of border drawn around the list box.
3.ColumnWidthGets of sets the width of columns in a multicolumn list box.
4.HorizontalExtentGets or sets the horizontal scrolling area of a list box.
5.HorizontalScrollBarGets or sets the value indicating whether a horizontal scrollbar is displayed in the list box.
6.ItemHeightGets or sets the height of an item in the list box.
7.ItemsGets the items of the list box.
8.MultiColumnGets or sets a value indicating whether the list box supports multiple columns.
9.ScrollAlwaysVisibleGets or sets a value indicating whether the vertical scroll bar is shown at all times.
10.SelectedIndexGets or sets the zero-based index of the currently selected item in a list box.
11.SelectedIndicesGets a collection that contains the zero-based indexes of all currently selected items in the list box.
12.SelectedItemGets or sets the currently selected item in the list box.
13.SelectedItemsGets a collection containing the currently selected items in the list box.
14.SelectedValueGets or sets the value of the member property specified by the ValueMember property.
15.SelectionModeGets or sets the method in which items are selected in the list box. This property has values −NoneOneMultiSimpleMultiExtended
16.SortedGets or sets a value indicating whether the items in the list box are sorted alphabetically.
17.TextGets or searches for the text of the currently selected item in the list box.
18.TopIndexGets or sets the index of the first visible item of a list box.
Properties and Description of the ListBox Control in VB.net

Methods of the ListBox Control in VB.net

The following are some of the commonly used Methods of the ListBox Control in VB.net.

#Methods of the ListBox Control in VB.netDescription
1.BeginUpdatePrevents the control from drawing until the EndUpdate method is called, while items are added to the ListBox one at a time.
2.ClearSelectedUnselects all items in the ListBox.
3.EndUpdateResumes drawing of a list box after it was turned off by the BeginUpdate method.
4.FindStringFinds the first item in the ListBox that starts with the string specified as an argument.
5.FindStringExactFinds the first item in the ListBox that exactly matches the specified string.
6.GetSelectedReturns a value indicating whether the specified item is selected.
7.SetSelectedSelects or clears the selection for the specified item in a ListBox.
8.OnSelectedIndexChangedRaises the SelectedIndexChanged event.
9.OnSelectedValueChangedRaises the SelectedValueChanged event.
Methods and Descriptions of the ListBox Control in VB.net

Events of the ListBox Control in VB.net

The following are some of the commonly used Events of the ListBox Control in VB.net.

#Events of the ListBox Control in VB.netDescription
1.ClickOccurs when a list box is selected.
2.SelectedIndexChangedOccurs when the SelectedIndex property of a list box is changed.
Events and Descriptions of the ListBox Control in VB.net

Let’s create a program to select an item from the ListBox in the VB.NET form.

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub Button1_Click_1(sender As Object, e As EventArgs)

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ListBox1.Items.Add("Red horse")
        ListBox1.Items.Add("Emperador")
        ListBox1.Items.Add("Fundador")
        ListBox1.Items.Add("Alfonso")
        ListBox1.Items.Add("San Mig")
        ListBox1.Items.Add("Tanduay")
    End Sub

    Private Sub Button1_Click_2(sender As Object, e As EventArgs)

    End Sub

    Private Sub Button1_Click_3(sender As Object, e As EventArgs) Handles Button1.Click
        Dim lt As String  ' define a local variable.  
        lt = ListBox1.Text  'accept the data from the ListBox1  
        MsgBox(" Selected Drinks is: " & lt)  ' Display the selected item
    End Sub
End Class

Program Output:

ListBox Control in VB.net Output
ListBox Control in VB.net Output

Now select an item from the list. We have selected Red horse.

ListBox Control in VB.net Selecting Item
ListBox Control in VB.net Selecting Item

Click on the Show button to display the selected item in Windows Form, as follows.

ListBox Control in VB.net Display Message Box
ListBox Control Display Message Box

Summary

In this article, we discussed How To Create a VB.net ListBox Control in Windows Forms using Microsoft Visual Studio at design-time as well as run-time. After that, we saw how to use various properties, events and methods.


Leave a Comment