The ListBox control in Visual Basic has some powerful features, making it useful in a number of applications. For example, the programmer can create a single column with a vertical scroll bar or several columns with horizontal scroll bars.
Scrolling in one ListBox is synchronizable with another ListBox so that both lists scroll together. Programmers may set up the ability to read in lengthy lists of terms from a text file and multiple items are selectable from a list.
- Skill level:
Prepare a form to hold a new ListBox control after opening Visual Basic by clicking on “File” and “New Project.” Choose “Standard EXE” for the template. Place a ListBox control on the form by clicking and dragging the control from the multiple controls in the left panel. The ListBox control is an icon that looks like a small rectangle containing three horizontal lines and a vertical scrollbar. It is usually the fifth icon down in the right hand column, just below a small circle with a dot in it. When you place the cursor over the box, “ListBox” will appear. On the form, use the mouse to enlarge the box until it fills about a third of the form.
Add a TextBox to the form by double clicking on the TextBox control in the left panel. This icon is a small square with the letters “ab” in it, usually the second down in the right column. Save the form with the two controls by clicking “File” and “Save Project as.” First, you will be prompted to enter a form name. Give it the name “ListBox Demo”. After clicking on “Save,” a second prompt asks for a project name. Give the project the same “ListBox Demo” name.
Create a list of places that will display the population when a location is clicked. Open the “Properties” panel on the right side of the screen (if it is not already visible) by pressing the “F4” key. Click on the entire form and then scroll down in the Properties until you see “Caption.” Double click on this to highlight the name, which will be “Form 1.” Change this to “Population.” Click on TextBox1 and change the MultiLine property to “True.”
Click “View” and then “Code” to open a code window. Fill the column with some data by typing the following lines exactly as shown:
Private Sub Form_Load() List1.AddItem "Washington" List1.AddItem "Seattle" List1.AddItem "Bellevue" List1.AddItem "Redmond" List1.AddItem "Oregon" List1.AddItem "Portland" List1.AddItem "Coos Bay" List1.AddItem "Myrtle Point" End Sub
Add some code that will display the population of a place when the user selects it by entering this code below the first routine: Private Sub List1_Click() Dim Population As String Population = " has a current population of " Select Case List1.ListIndex Case "0" Text1.Text = "Washington" & Population & "6,5498,224." Case "1" Text1.Text = "Seattle" & Population & "582,174." Case "3" Text1.Text = "Redmond" & Population & "48,739." End Select End Sub
Save the project again. Press “F5” to test the project. When you highlight a place with the cursor, the population data should appear in the TextBox. If not, recheck your code entry to ensure it matches exactly what you see in the preceding steps.
Tips and warnings
- The above code illustrates how to use information selected from a ListBox to display other data. The actions illustrated could equally well be used to pass the selected item to different routine, for example, feeding into a reservation system.
- Use two ListBox controls to work with two mutually exclusive columns and move data from one to the other.
- Set the Style of the control to “1 – Checkbox” to add checkboxes to each item.
- Be careful when using the “ListIndex” property. In Visual Basic the index starts with “0” not “1.”
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for