articles

Home / DeveloperSection / Articles / ListBox Control in VB.Net

ListBox Control in VB.Net

Pushpendra Singh10641 11-Dec-2010


The ListBox control displays a list of items from which we can make a selection. We can select one or more than one of the items from the list.

Drag and drop ListBox from toolbox on the window Form.

ListBox Control in VB.Net

Code:

Public Class Form8
 
    Private Sub Form8_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Item is added in ListBox
        ListBox1.Items.Add("C#")
        ListBox1.Items.Add("J#")
        ListBox1.Items.Add("VB")
        ListBox1.Items.Add("Java")
    End Sub
 
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        'selected option will show in Label using the selected item property
        Label2.Text = "Selected Language is  " + ListBox1.SelectedItem
    End Sub
End Class

In above code Item is added dynamically so all Item in ListBox1  will show at run time.

Run the project

 

ListBox Control in VB.Net

 

When you select given option then SelectedIndexChanged event will fire and selected Item will show in the Label.

 

ListBox Control in VB.Net

 

ListBox Control Properties

Sorted:

ListBox can be sorted through ListBox sorted Property

Private Sub Form8_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' sorted ListBox Item
        ListBox1.Sorted = True    

ListBox Control in VB.Net

ListBox Item is sorted.

BackColor:

ListBox Color can be changed through ListBox BackColor Property

Private Sub Form8_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'change ListBox BackColor
        ListBox1.BackColor = Color.LawnGreen
End Sub

ListBox BackColor will be change when Application Run.

ListBox Control in VB.Net

ForeColor:

ListBox Fore color is changed through ListBox ForeColor Property.

Private Sub Form8_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'change ListBox ForeColor
        ListBox1.ForeColor = Color.Red
End Sub

ListBox Fore color is changed at run time.

ListBox Control in VB.Net


Updated 04-Mar-2020

Leave Comment

Comments

Liked By