ComboBox Control in VB6
A ComboBox control combines the features of a TextBox control and a ListBox control.
Contents
About the ComboBox
Users can enter information in the text box portion or select an item from the list box portion of the control.
Use a combo box when you need a user to either select a list of suggested alternatives or to type his own , and use a list box if the user is required to select from a fixed number of items.
There is only one single type of a list box, but there are three types of combo boxes which can be selected with the style property value of 0(Drop Down Combo Box), 1(Simple Combo Box), and 2(Drop-Down List Box).
To add or delete items in a ComboBox control, use the AddItem or RemoveItem method. Set the List, ListCount, and ListIndex properties to enable a user to access items in the ComboBox. Alternatively, you can add items to the list by using the List property at design time.
A Scroll event will occur in a ComboBox control only when the contents of the dropdown portion of the ComboBox are scrolled, not each time the contents of the ComboBox change. For example, if the dropdown portion of a ComboBox contains five items and the top item is highlighted, a Scroll event will not occur until you press the down arrow six times (or the PGDN key once). After that, a Scroll event occurs for each press of the down arrow key. However, if you then press the up arrow key, a Scroll event will not occur until you press the up arrow key six times (or the PGUP key once). After that, each up arrow key press will result in a Scroll event.
Properties
Items are added or removed to combo and list boxes during both runtime and design time. If you want to add list items at design time, select the list property.
AddItem
List1.additem "Hello World" 'Add items List1.additem "This is 2nd Item" List1.additem "First item",0 'squeeze into 1st
RemoveItem
list1.removeitem 3 'Remove the fourth item
Clear
ist1.clear 'Clear the list box
ListCount
The number of items in a list is given by the listcount property.
ListIndex
The currently selected item's index can be returned with the listindex property. A listindex property of -1 indicates that no item has been selected
Multi-Columns Combo Box
The Visual BASIC 6.0 ComboBox does NOT support multiple columns. However, versions of Visual Studio do include components with a ComboBox that does support multiple columns.
To add support for multiple columns in a combo box, in the VB6.0 IDE you can press CTRL-T, or choose "References" from the "Project" menu, or you can right click on the components tool box and select "Components"... then scroll down though the components list, find "Microsoft Forms 2.0 Object Library" and check the box.
By adding the "Microsoft Forms 2.0 Object Library" to your components, you will find an additional ComboBox in the IDE tool box. Use that Combo box for multiple columns support. If your version of Visual BASIC does not have this library, you can install Microsoft Office, Excel, or Access (version 2000 has it verified) and the component will be added to Visual BASIC.
- You will need "Microsoft Forms 2.0 Object Library" to have the multiple column combo box.
- The "Microsoft Forms 2.0 Object Library" file is FM20.dll
- The "Microsoft Forms 2.0 Object Library" is included with versions of Microsoft Office, and Microsoft Excel with VBA support.
Examples Section:
Private Sub Command1_Click() Dim Index As Long Index = ListBox1.ListCount Call ListBox1.AddItem("Column1", Index) ListBox1.List(Index, 1) = "Column2" ListBox1.List(Index, 2) = "Column3" End Sub
cboEData(4).AddItem rs1!PLValue cboEData(4).List(0, 1) = rs1!PLID
cboBoxName.Clear cboBoxName.ColumnCount = 2 cboBoxName.ListWidth = "13 cm" cboBoxName.ColumnWidths = "3 cm; 10 cm" cboBoxName.ColumnHeads = True cboBoxName.Clear cboBoxName.AddItem "P001" cboBoxName.List(0, 1) = "Product Name"
Forms 2.0 ComboBox Properties
Additional properties available in the "Microsoft Forms 2.0 Object Library" Multi-Columns Combo Box.
TextColumn
You can use the TextColumn property of the ListBox or ComboBox control to display one set of values to your user in a list, but to return another value based on the selection that the user makes.
Create a form, add 3 controls, a ComboBox "cboList", a CommandButton "cmdSubmit", and a multiline TextBox "txtOut".
Option Explicit Private Sub cmdSubmit_Click() cboList.TextColumn = 1 txtOut.Text = "values: " & vbCrLf & vbCrLf txtOut.Text = txtOut.Text & "text = " & cboList.Text & vbCrLf & vbCrLf cboList.TextColumn = 2 txtOut.Text = txtOut.Text & "text = " & cboList.Text & vbCrLf & vbCrLf End Sub Private Sub Form_Load() cboList.ColumnCount = 2 cboList.ColumnWidths = "40;40" Call cboList.AddItem("Lisa", 0) cboList.List(0, 1) = "Smith" Call cboList.AddItem("Stanley", 1) cboList.List(1, 1) = "Smith" Call cboList.AddItem("Ben", 2) cboList.List(2, 1) = "Nelson" Call cboList.AddItem("Stacy", 3) cboList.List(3, 1) = "Springs" End Sub
Study the example above. Note how the selected text changes. Also note how what is displayed in the combo box changes.
Simulate the way a ComboBox works on a Web Form!
Option Explicit Private Sub cmdSubmit_Click() cboList.TextColumn = 1 txtOut.Text = "values: " & vbCrLf & vbCrLf txtOut.Text = txtOut.Text & "Employee Code = " & cboList.Text & vbCrLf cboList.TextColumn = 2 txtOut.Text = txtOut.Text & "Employee Name = " & cboList.Text & vbCrLf & vbCrLf End Sub Private Sub Form_Load() cboList.ColumnCount = 2 cboList.TextColumn = 2 cboList.ColumnWidths = "0;80" cboList.Text = "Ben Nelson" Call cboList.AddItem("EMP002", 0) cboList.List(0, 1) = "Lisa Smith" Call cboList.AddItem("EMP006", 1) cboList.List(1, 1) = "Stanley Smith" Call cboList.AddItem("EMP019", 2) cboList.List(2, 1) = "Ben Nelson" Call cboList.AddItem("EMP012", 3) cboList.List(3, 1) = "Stacy Springs" End Sub
- Sets a default value in the combo box.
- Hides the Employee ID from view.
- When a name is selected, the employee ID value is captured.