How To Use A Listbox In Visual Basic
To use a ListBox in Visual Basic, add the control to a Windows Forms form, populate its Items collection or bind a data source, and choose a selection mode. Read SelectedItem for a single choice or SelectedItems for multiple choices, then handle SelectedIndexChanged to update.
Which licences fit your PC setup?
Prices are an AUD snapshot, not a live quote. Confirm variant and total on the listing.
-

Windows 11 Pro key
$14.99 AUD
$29.00 -

Office 2024 Pro Plus key
$20.00 AUD
$39.00 -

Windows 11 Home key
$18.88 AUD
$37.00
Add a ListBox to a Windows Forms project
This guide uses VB.NET and the Windows Forms ListBox control in Visual Studio. The property names and examples apply to that control; other Visual Basic environments have different APIs. For related language lessons, browse the Visual Basic tutorials.
- Open a Visual Basic Windows Forms project and open your form in the designer.
- Drag a ListBox from the Toolbox onto the form.
- Set its Name property to ListBox1 so the expressions below match your control.
- Adjust its size and position. Add a Label named Label1 if you want to display the current selection.
- Set SelectionMode to One for the initial exercise.
A ListBox suits a visible list of choices, such as categories, file names or tasks. Give it enough space for users to recognise the options and see which item is selected.
Populate the list with items
Enter fixed choices in the designer
- Select ListBox1 and find Items in the Properties window.
- Open the collection editor using the ellipsis button.
- Enter Apple, Banana and Orange on separate lines, then confirm the entries.
- Run the application and check that each choice appears.
Design-time entries are useful when the choices are known in advance. For information that changes while the application runs, populate the control in code or use data binding.
Add items while the application runs
Inside the form's Load event handler, ListBox1.Items.Add("Apple") adds one entry. ListBox1.Items.AddRange(New Object() {"Apple", "Banana", "Orange"}) adds the sample choices together.
Choose one initialisation method for this exercise. Adding the same fruit names in both the designer and Load handler creates duplicate entries. If you intentionally rebuild an unbound list, call ListBox1.Items.Clear() before adding its replacement contents.
Bind a collection when the data already exists
An alternative is ListBox1.DataSource = New String() {"Apple", "Banana", "Orange"}. For a collection of objects, DisplayMember identifies the property shown to users, while ValueMember identifies the property available through SelectedValue.
Once DataSource is assigned, update the underlying data rather than calling Items.Add, Items.Remove or Items.Clear. Microsoft documents this restriction in its DataSource reference. Automatic refresh depends on the source and its change notifications; assigning an ordinary collection does not guarantee that later edits will appear automatically.
Choose how users select entries
SelectionMode controls both the number of permitted choices and the interaction used to select them.
| SelectionMode value | Behaviour | Suitable use |
|---|---|---|
| One | Allows one selected item. | Choosing one category or destination. |
| MultiSimple | Clicking an item or pressing Space toggles its selection. | Choosing several independent options without modifier keys. |
| MultiExtended | Ctrl-click toggles individual items; Shift-click extends a selection range. | Selecting separate entries or adjacent groups. |
| None | Prevents item selection. | Displaying a list that users do not need to select. |
For example, ListBox1.SelectionMode = SelectionMode.MultiExtended enables extended multiple selection. These behaviours are described in Microsoft's SelectionMode documentation.
Read the selection and respond to changes
Handle a single selected item safely
SelectedItem returns the selected object. SelectedIndex gives its zero-based position: the first item is at index 0, and a value of -1 means nothing is selected.
- Open the form's code editor and create a handler for ListBox1.SelectedIndexChanged using the control and event dropdowns.
- At the start of the handler, use If ListBox1.SelectedItem Is Nothing Then Exit Sub.
- On the following line, use Label1.Text = ListBox1.SelectedItem.ToString() to show the selected text.
- Run the form and select each fruit to check that the label changes.
The empty-selection check prevents an attempt to call ToString() on Nothing. If your interface should clear its label when the selection disappears, set Label1.Text to an empty string before exiting that branch.
Use SelectedIndexChanged for this Windows Forms event. It can also run when code changes the selection, including during population, so avoid assuming that every event represents a deliberate user click.
When testing whether a selection exists, If ListBox1.SelectedIndex <> -1 Then checks for a valid selected index. The guide to the Visual Basic does-not-equal operator explains that comparison syntax.
Read every selected item
For multiple selection, iterate over ListBox1.SelectedItems with For Each item As Object In ListBox1.SelectedItems, perform your action inside the loop, and close it with Next. SelectedIndices provides the selected positions when you need indexes.
SelectedItem alone does not represent the complete multiple selection. Use a button to process the collection when users need time to finish choosing several entries.
Remove entries and reset the list
For an unbound ListBox, choose the operation that matches the change:
- ListBox1.Items.Remove("Apple") removes a matching entry.
- ListBox1.Items.RemoveAt(0) removes the first entry, provided the list contains an item.
- ListBox1.Items.Clear() removes all entries.
- ListBox1.ClearSelected() clears the selection while retaining the entries.
To remove a selected item by index, check that SelectedIndex is not -1 before passing it to RemoveAt. If removing several selected entries, collect their indexes first and remove them from highest to lowest so earlier removals do not shift the remaining targets.
Adjust appearance and ordering
Use Font, ForeColor and BackColor to customise text and colours while preserving readable contrast. Test long entries and keyboard navigation as well as short sample labels.
For an unbound list, ListBox1.Sorted = True sorts strings alphabetically. For a bound list, arrange the data in the source, following Microsoft's sorting guidance. Sorting changes positions, so avoid treating an item's index as its permanent identifier.
MultiColumn flows entries into additional columns; it does not create separate data fields for each row. Use a DataGridView when users need a table with distinct fields.
For custom item rendering, OwnerDrawFixed uses DrawItem. Variable-height items require OwnerDrawVariable together with MeasureItem and DrawItem, as explained in Microsoft's owner-drawing guide. Keep normal drawing until a specific display requirement justifies that extra code.
Practise tracing a selection through your code
When reading other projects, including How to Code a Virus in Visual Basic, distinguish the interface event handler from the application logic it calls. A ListBox presents choices; the handler determines what happens after a choice changes.
Next, add the three fruit choices to a test form and connect SelectedIndexChanged to Label1. Verify that selecting an item updates the label and clearing the selection causes no error.
Other editions for separate workloads
Other editions for separate workloads; prices in AUD, confirm the total on the listing.
-

Windows 10 Pro key
$12.00 AUD
$15.00 -

Microsoft 365 Pro Plus, 5 devices
$15.00 AUD
$29.00 -

Windows Server 2022 Standard
$120.00 AUD
$239.00
How to check this worked
- Open Settings, System, Activation and confirm the edition shown.
- Enter the key and run activation; expect a green tick within a minute.
- Reboot and re-check activation survives the restart.
FAQ
What else should you know before buying?
- What do I need before starting?
- Read the requirements and confirm the edition first; the steps assume the machine is compatible.
- How do I know the licence is genuine?
- Activate on Microsoft's servers and check Settings, System, Activation. A working activation is the test.
- Is this a subscription?
- Keys sold here are one-time lifetime licences. Microsoft 365 plans are billed by Microsoft instead.
- What if the key fails?
- Email hi@ms.codes with the order number and a screenshot. Replacement or refund applies within 30 days.
Check who prepared and reviewed this guide
- Written by
- MS.Codes editorial
- Reviewed by
- Support team
Last verified: (source review; see test status above).