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

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.


Code to add items via an InputBox:
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.


Code to remove specific items:
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

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.


Code to add items to a ComboBox:
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.


Code to remove items from a ComboBox:
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

Visual Basic 2019 Made Easy
Unlock the power of Visual Basic 2019 with this comprehensive, easy-to-follow handbook written by Dr. Liew, renowned educator and founder of the popular programming tutorial website VBtutor.net. Whether you're new to programming or brushing up your skills, this book is your perfect companion to learn Visual Basic 2019 from the ground up.
What You'll Learn:
- Understand Core Programming Concepts: Grasp the foundational principles of Visual Basic 2019, including variables, data types, conditional logic, loops, and event-driven programming.
- Develop Real Windows Desktop Applications: Build fully functional and interactive Windows apps using Visual Studio 2019—guided through step-by-step tutorials.
- Apply Dozens of Ready-to-Use Examples: Explore a rich collection of practical sample programs, from basic calculators to image viewers and database applications.
- Adapt and Reuse Code for Your Own Projects: Customize professionally written code snippets to speed up your development process and bring your ideas to life.
- Package and Deploy Like a Pro: Learn how to compile, test, and distribute your Visual Basic applications seamlessly with built-in deployment tools.

Visual Basic Programming With Code Examples
Visual Basic Programming with Code Examples offers a unique dual-format approach, showcasing sample codes in both Visual Basic 6 (VB6) and VB.NET. This side-by-side presentation helps you understand the evolution of Visual Basic and empowers you to work confidently across both environments.
What You'll Learn:
- Core Concepts Made Easy: Explore data types, control structures, file handling, procedures, user interface design, and more.
- Hands-On Application Building: Design real-world applications, including financial calculators, educational tools, games, multimedia apps, and database systems.
- 48 Practical Code Examples: Study and customize fully explained programs that illustrate key programming techniques.
- Dual-Code Format: Learn to translate and adapt code between VB6 and VB.NET seamlessly.