Lesson 6: ListBox and ComboBox in VB2019

Master list-based controls to display, manage, and interact with collections of items in your applications

Key Takeaway

ListBox and ComboBox controls provide powerful ways to display and manage lists of items. Mastering these controls enables you to create professional applications with intuitive user interfaces for data selection and management.

In Visual Basic 2019, ListBox and ComboBox controls are essential for presenting lists of items to users. While both display collections, they differ in functionality and appearance. This lesson explores how to effectively implement and manipulate these controls in your applications.

6.1 The ListBox Control

The ListBox control displays a scrollable list of items that users can select from. It's ideal for showing multiple items simultaneously and allowing single or multiple selections.

Adding Items

Add items at design time or programmatically at runtime

Removing Items

Delete specific items or clear the entire list

Selection Modes

Configure for single or multiple item selection

Sorting

Automatically sort items alphabetically

ListBox String Collection Editor
Figure 6.1: Adding items to ListBox using String Collection Editor

6.1.1 Adding Items to a ListBox

You can add items to a ListBox either at design time using the String Collection Editor or programmatically at runtime using the Add() method.

ListBox with items
Figure 6.2: ListBox displaying items
ListBox InputBox
Figure 6.3: InputBox for adding items

Code to add items via an InputBox:

Form1.vb
Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
    ' Declare a variable to store the new item
    Dim newItem As String
    
    ' Prompt user for input using an InputBox
    newItem = InputBox("Enter item to add:")
    
    ' Add the item to the ListBox
    If newItem <> "" Then
        ListBox1.Items.Add(newItem)
    End If
End Sub

Output:

6.1.2 Removing Items from a ListBox

You can remove items individually or clear the entire ListBox using the Remove() and Clear() methods.

Delete item InputBox
Figure 6.4: Deleting items via InputBox
ListBox after deletion
Figure 6.5: ListBox after item removal

Code to remove specific items:

Form1.vb
Private Sub BtnRemove_Click(sender As Object, e As EventArgs) Handles BtnRemove.Click
    ' Declare a variable to store the item to remove
    Dim removeItem As String
    
    ' Prompt user for item to remove
    removeItem = InputBox("Enter item to remove:")
    
    ' Remove the item if it exists
    If removeItem <> "" AndAlso ListBox1.Items.Contains(removeItem) Then
        ListBox1.Items.Remove(removeItem)
    Else
        MsgBox("Item not found in the list!")
    End If
End Sub

Output:

Important Notes

SelectionMode Property: Determines how users select items (None, One, MultiSimple, MultiExtended).

SelectedIndex Property: Gets or sets the zero-based index of the currently selected item.

SelectedItem Property: Gets or sets the currently selected item in the ListBox.

6.2 The ComboBox Control

The ComboBox control combines a textbox with a drop-down list. Users can select from predefined options or enter their own value (when DropDownStyle is set to DropDown).

DropDown Styles

Simple, DropDown, and DropDownList options

Adding Items

Similar methods to ListBox for item management

AutoComplete

Enable automatic suggestions as user types

Selection Events

Respond to user selections with SelectedIndexChanged

ComboBox with items
Figure 6.6: ComboBox with items added at design time

6.2.1 Adding Items to a ComboBox

Adding items to a ComboBox is similar to the ListBox. You can use the String Collection Editor at design time or the Add() method at runtime.

ComboBox dropdown
Figure 6.7: ComboBox drop-down list
ComboBox InputBox
Figure 6.8: Adding items via InputBox

Code to add items to a ComboBox:

Form1.vb
Private Sub BtnComboAdd_Click(sender As Object, e As EventArgs) Handles BtnComboAdd.Click
    ' Declare variable for new item
    Dim newItem As String
    
    ' Get input from user
    newItem = InputBox("Enter item to add to ComboBox:")
    
    ' Add item if not empty
    If newItem <> "" Then
        ComboBox1.Items.Add(newItem)
    End If
End Sub

Output:

6.2.2 Removing Items from a ComboBox

Removing items from a ComboBox follows the same pattern as the ListBox, using the Remove() and Clear() methods.

ComboBox deletion
Figure 6.9: Deleting items via InputBox
ComboBox after deletion
Figure 6.10: ComboBox after item removal

Code to remove items from a ComboBox:

Form1.vb
Private Sub BtnComboRemove_Click(sender As Object, e As EventArgs) Handles BtnComboRemove.Click
    ' Declare variable for item to remove
    Dim removeItem As String
    
    ' Get item to remove from user
    removeItem = InputBox("Enter item to remove from ComboBox:")
    
    ' Remove item if it exists
    If removeItem <> "" AndAlso ComboBox1.Items.Contains(removeItem) Then
        ComboBox1.Items.Remove(removeItem)
    Else
        MsgBox("Item not found in the ComboBox!")
    End If
End Sub

Output:

Important Notes

DropDownStyle Property: Determines the appearance and behavior of the ComboBox (Simple, DropDown, DropDownList).

AutoCompleteSource: Specifies the source of complete strings for automatic completion.

AutoCompleteMode: Controls how automatic completion works (None, Suggest, Append, SuggestAppend).

Interactive List Demo

Try this interactive demo to understand how ListBox and ComboBox controls work:

ListBox Items

    Lesson Summary

    In this lesson, you've learned how to effectively work with ListBox and ComboBox controls:

    ListBox Control

    Display and manage scrollable lists of items with selection capabilities

    ComboBox Control

    Create dropdown lists that combine selection and input capabilities

    Item Management

    Add and remove items at both design time and runtime

    Practical Applications

    Implement list controls for selection menus, configuration options, and data display

    You've learned how to work with two powerful list controls that are essential for creating interactive applications. In the next lesson, we'll explore how to work with images using the PictureBox control.

    Next Lesson

    Ready to learn about image handling? Continue to Lesson 7: Working with PictureBox.

    Related Resources

    VB6 Tutorial

    Mastering VB6 Programming

    Explore Tutorials

    Visual Basic Examples

    Practical VB code samples for real-world applications

    View Examples

    Excel VBA Tutorial

    Learn how to automate Excel by creating VBA macros

    Learn More

    VB2019 Paperback

    Comprehensive guide to Visual Basic 2019

    Get on Amazon