VB.NET ListBox - Introduction

A ListBox is a windows form component that provides users with a selectable list of items.

With a ListBox users can select one or more items.

In a ListBox, if the total number of items to be rendered exceeds the number that can be displayed in the viewport, then automatically a scrollbar is added.

Alot of people don't know that ListBoxes can also displayed data in multiple columns like ListViews and DataGridView. To do that the MultiColumn has to be set to true.

By default it's always set to false hence displaying data in a single column.

If you prefer that the scrollbar should always appear, then you set the ScrollAlwaysVisible to true.

You can determine how many rows of ListBox items that can be selected at any time using the SelectionMode property.

If you want to get the selected row in the ListBox, you can use the SelectedIndex property which gives you an integer value representing the first selected row.

If no item i selected, then the SelectedIndex defaults to -1.

However, if you just want the selected object but not it's index, then you can use the SelectedItem property.

You can get the total number of items renderd in your ListBox via the Count property.

You can perform CRUD operations in the ListBox with provided methods. To add an item to ListBox you can use the Add(), to insert an item you can use the Insert(), to remove you use Remove() while to clear all items in the ListBox you use the Clear() method.

ListBox API Definition

ListBox is a public class defined in the in the System.Windows.Forms namespace.

Namespace System.Windows.Forms;

That namespace itself belongs to System.Windows.Forms.dll assembly.

As a class ListBox derives form System.Windows.Forms.ListControl

Public Class ListBox Inherits System.Windows.Forms.ListControl

ListControl is an abstract class that provides common implementation of members for the ListBox and the ComboBox classes.

When you work with a ListBox, the Windows System will automatically draw the ListBox items for you.

However, you can override this behavior using the DrawMode property of the ListBox, then handle the MeasureItem and DrawItem events.

If you have massive amounts of items that need to added to your ListBox, performance may be impacted given that each item addition leads to the control being repainted.

However, you can solve that by using the BeginUpdate and EndUpdate methods. These allow you to add those data without the control being repainted every time an item is added. This makes it efficient.

You can also search a ListBox using the FindString and FindStringExact() methods.

You can also bind items to a ListBox using the DataSource property.


Let's look at an example to populate a ListBox from an Array of data.


Imports System
Imports System.Drawing
Imports System.Windows.Forms

Module Program

    Private WithEvents myListBox As ListBox = New ListBox With {.Location = New Point(30, 70), .Size = New Size(500, 400), .Font = New Font(FontFamily.GenericSansSerif, 15)}

    Private Sub populateListBox()
        Dim nebulae As String() = {"Horse Head", "Black Widow", "Ghost Head", "Cat's Eye", "Elephant's Trunk", "Helix", "Rosette", "Snake", "Bernad 68", "Ant", "Orion", "Butterfly", "Eagle", "Own", "Ring", "Pelican", "Cone", "Flame", "Witch Head", "Bumerang"}
        For Each nebular As String In nebulae
    End Sub

    Private Sub myListBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles myListBox.SelectedIndexChanged
            Dim selectedIndex As Integer = myListBox.SelectedIndices(0)
            If selectedIndex <> -1 Then
                Dim name As String = myListBox.Items(selectedIndex).ToString()
            End If
        Catch argumentOutOfRangeException As ArgumentOutOfRangeException
        End Try
    End Sub

    Private Sub createForm()
        Dim myForm As Form = New Form With {.Text = "VB.NET WinForms : ListBox - Fill From Array - Camposha.info", .ClientSize = New Size(564, 520), .BackColor = Color.MediumSeaGreen}
    End Sub

    Sub Main()
    End Sub
End Module


VB.NET ListBox

Best Regards.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:


What do You Think

Previous Post Next Post