How to Set Index Values of ComboBox for VB?

Written by lora covrett
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Set Index Values of ComboBox for VB?
Pre-populated ComboBox controls help users work more effeciently because they don't have to type. (Comstock/Comstock/Getty Images)

A ComboBox is a control that can be added to a (Visual Basic 6) VB6 form. The ComboBox control has three styles: "0 - Drop-down Combo," "1 - Simple Combo" and "2 - Drop-down List." The "Dropdown Combo" and "Simple Combo" styles allow the user to type in the control. The "Simple Combo" only displays a single value and acts more like a TextBox control. The "Dropdown List" style does not allow the user to type in the control. This is usually the best option when the program needs to use the selected text elsewhere. All styles allow the programmer to access the contents of the control by text or by an integer index value.

Skill level:
Moderate

Other People Are Reading

Instructions

  1. 1

    Create a new Standard EXE VB project. Select the "ComboBox" control and then click on the "Form1" form that was created by default to add it. You will now have a ComboBox named "Combo1" on your form.

  2. 2

    Rename the ComboBox to something meaningful. If the contents of the control will be U.S. state abbreviations, name the control "cmbStates." The default style of the ComboBox is "0 - Drop-down Combo." To not allow the user to type in a different state, change the style to "2 - Drop-down List."

  3. 3

    Populate the contents of the "cmbStates" ComboBox. If the data is stored in a database, retrieve the data into a RecordSet and use the "AddItem" method of the ComboBox control to add each state abbreviation. The code will look similar to: "cmbStates.AddItem(oRecordSet.Fields("State_abbrev").Value, i)" -- where "oRecordSet" is the name of the RecordSet returned from the database query, "State_abbrev" is the column name in the table, and "i" begins at zero and is incremented by one for every record processed.

  4. 4

    Access a specific value in the populated ComboBox either by text value or index value. The index value is the "ListIndex" property. The code to set the visible contents of the ComboBox to the fifth value of the ComboBox is "cmbStates.ListIndex(4)."

  5. 5

    Specify an index value for a particular entry by manually coding "cmbStates.AddItem("OH", 17)." The program requirements might be to add each state abbreviation indexed by the order in which they joined the Union. If another area of the program asks for the seventeenth that joined the Union, the code would look like "cmbStates.ListIndex(17)" and the value displayed in the control would be "OH." To retrieve the state abbreviation and store in a variable: "sSeventeenthState = cmbStates.List(17)."

Tips and warnings

  • The index values always begin at zero by default.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.