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:
Other People Are Reading
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.
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."
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.
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)."
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.
- 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