How to Create a Combo Box in Visual Basic

By eHow Internet Editor

Rate: (7 Ratings)

Many programmers use Visual Basic when they want to create an interface that requires information from the user, and in situations where you want to give the user a list of options but still afford the possibility of an unforeseen choice, the combo box is the tool you want. Combo boxes can display a list of choices, either preset in code or built "on the fly," but they also allow users to input their own entry. Once the combo box has been created and filled out, your code can determine what was entered, its position on the list, and whether or not the user entered a new, unlisted value. Combo boxes can also be linked to read and write data to a field in a database.

Instructions

Difficulty: Easy

Create the Combo Box

Step1
Go to the "Toolbox" palette. Find the tool called "Combo Box" and double-click on it. This will create a combo box.
Step2
Resize the combo box as needed using the resizing handles (which appear as small squares around the perimeter of the box). Drag on any of these to stretch or shrink the check box's size (you can only adjust the width, not the height). By clicking on and dragging the combo box as a whole, you can move it around the screen.

Name the Combo Box

Step1
Look for the "Name" property in the properties window. Of the two columns that appear, click in the column next to "Name" and type in a single word that you will later use to identify the box.
Step2
Begin combo box names with "cbo" followed by a capitalized short word that describes the combo box's function. If the combo box will be linked to a database, consider naming it "cbo" plus the name of the field it will be linking to.

Populate the Combo Box From a Database

Step1
Set the "Datasource" property to the name of a data control (created on your form previously) that represents the database you are linking to. You can set this property in the same manner that you set the "Name" property previously.
Step2
Set the "Datafield" property to the name of the specific field that this combo box should be linked to. You can set this property in the same manner that you set the "Name" property previously.

Populating the Combo Box From a List

Step1
Set the "List" property to a string of alphanumeric values that you want to be listed as options in the combo box, typing CTRL-ENTER between each pair of values.
Step2
Set the "Text" property to a string of alphanumeric values that you want the combo box to display when the form is first viewed.

Tips & Warnings

  • Creating a clear and obvious label for the combo box so you remember what it is supposed to hold.
  • Make sure you create combo boxes that are wide enough to hold any kind of data entry. That way you ensure the display won't be cut off.
  • It's wisest to label the combo box immediately after creating it, before you being writing code for or about the combo box. If you forget to label the combo box, it will make it difficult to find later when reading the code.

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 Combo 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.