How to Create a List Box in Visual Basic

By eHow Internet Editor

Rate: (7 Ratings)

When you want to request information from the user, you can create an interactive interface using Visual Basic. Creating a list box is useful in situations where you want to give the user a list that does not leave room for an unforeseen choice. These list boxes can either be preset in code or built "on the fly," displaying the choices in a box of any size, scrollable if necessary. List boxes can also allow the user to select more than one option at at time. Once the list box has been created and filled out, your code can determine which entry was chosen by the user and its numerical position on the list. List boxes can also be linked to read and write data to a field in a database.

Instructions

Difficulty: Easy

Create the List Box

Step1
Go to the "Toolbox" palette and select the "List Box" tool. You can find this tool by hovering your mouse over each tool and noting the yellow tags that pop up and identify the names of the different tools.
Step2
Double-click on the "List Box" tool once you've found it. This will create a list box, which you can then resize.
Step3
Drag the resizing handles to stretch or shrink the list box's size. If a list box isn't tall enough to show all its entries, it will be scrollable.
Step4
Click on the interior of the list box to drag the entire box to different page position.

Name the List Box

Step1
Select the list box. Click on the resizing handles to make sure the box is selected.
Step2
Browse the "Properties" window. You want to find the "Name" property.
Step3
Name the text box by clicking in the column next to "Name," and typing in a single alphanumeric word. This is what you will use to find the list box in later code.
Step4
Use the standard conventions when naming the list box by beginning with "lst" and following it with a capitalized short word that describes the list box's function. If you plan to link the list box will to a database, consider naming it "lst" and the name of the field it will be linking to.

Populate the List Box From a Database

Step1
Set the "Datasource" property to the name of a data control (created on your form previously), which should represent the database you are linking to. This property can be set by following the same process you used to set the "Name" property.
Step2
Set the "Datafield" property to the name of the specific field you want this list box to link to. This property can also be set by applying the same process used to set the "Name" property.

Populate the List Box From a List

Step1
Populate the "List" property by setting it to a string of alphanumeric values and typing CTRL-ENTER between each pair of values. These will be listed as options in the list box.
Step2
Populate the "Text" property by setting it to a string of alphanumeric values that you want the list box to display when the form is first viewed.

Tips & Warnings

  • Make sure to create a logical label for your list box, preferably one that reflects its value.
  • Be sure to plan for the appropriate width.
  • It's important to name the list boxes immediately upon creation. Code can become unreadable if it's cluttered with too many unnamed or numbered objects.

Post a Comment

POST A COMMENT

Request a New How-To Article

Looking for more How To information? Chances are there’s an eHow member who knows how to do what you’re looking to do. Submit an article request now!

eHow Article:  How to Create a List Box in Visual Basic

eHow Internet Editor

eHow Internet Editor

Category: Internet

Articles: See my other articles

Related Ads

Internet

Veesites
Meet Virginia DeBolt eHow’s Internet Expert.