The ListBox control in Visual Basic is a powerful tool for displaying lists of data and allowing the user to select one or more items from the list. A selection can then force an action in a different section of the code. There are multiple properties available with this control including ones to display data in more than one column. For longer or wider text lists, horizontal and vertical scroll bars appear automatically. Lists can appear sorted or unsorted and forms can have multiple interconnected ListBoxes.
Creating a ListBox
Load a new form by clicking the "File" menu in Visual Basic and selecting "New Project." Select "Standard EXE" as the type in Visual Basic 6.0. In versions of Visual Basic.NET, the name of the template is "Windows Forms Application." Once a form is open, you see two controls on the left that look like lists. Double-click on the right-hand one, a ListBox, to add it to the blank form. Repeat this to add a second ListBox, and then drag it to the right of the first one. Expand the form using the sizing handles at the bottom and right of the form.
Experiment with the ListBox contents and its properties. Change the "Sorted" property of each ListBox to "True." Change the ForeColor of the right-hand ListBox to "Red." To prepare for the next step in the tutorial, add four Command buttons from the Toolbox arranging them as in the image. Note: the text in the boxes is set larger for visibility. Using the Properties panel on the right, make each Command button caption read as follows:
Top Left: "Add New Item"
Top Right: "Select an Item"
Bottom Left: "Print Selected List"
Bottom Right: "Deselect an Item"
The uses of these buttons will become clear as you work through the tutorial.
Setting Up the Tutorial
Change from the Design view to the Code view by clicking on "View," "Code" in the menu. To activate the command buttons added above, type in the following code exactly as it appears:
Private Sub Command1_Click()
Dim NewItem As String
NewItem = InputBox("Enter new item for list")
Private Sub Command2_Click()
If List1.ListIndex >= 0 Then
Private Sub Command3_Click()
If List2.ListIndex >= 0 Then
Private Sub Command4_Click()
Dim n As Integer
For n = 0 To List2.ListCount - 1
Print n & " " & List2.List(n)
Using the ListBox Control
The ways to use these buttons will be obvious once you press "F5" to run the program (the illustration has been pre-filled with some items). Start by using the "Add New Item" command. Each time you use this, a box will prompt you to add a new item. Try adding some names of countries in random order. "Select an Item" removes it from the left box and adds it to the right. "Deselect an Item" does the reverse. The "Print" button sends it to the screen but by a modification could use a printer instead. Additional code can change the selection so that by holding down the shift key the user can select multiple items.
ListBox properties include linking to a database for further use of selected data. Boxes can be populated either one item at a time as shown here, by using an array of items to add all at once, or by importing a pre-selected text file. This is the way those long lists of countries appear in Web versions of the ListBox.
- Programming Microsoft Visual Basic 6.0; Francesco Balena, 1999
How to Use a Listbox Control in MFC Visual C++
A list box is a Windows control that can display text or iconic items as a list of rows. The Microsoft Foundation...
How to Clear Listbox
Clearing a list box is an action often associated with computer programming and specifically programming involving the Visual Basic programming language. Visual...
Visual Basic Controls & Functions
Visual Basic controls provide developers with the tools that retrieve and prompt users for software commands. Visual Basic has several common controls...
Visual Basic ListView Tutorial
The ListView control in Visual Basic became popular with Windows Explorer. This control works in conjunction with the TreeView control and is...