Changes

ComboBox Control in VB6

4,689 bytes added, 03:53, 29 January 2008
/* ListIndex */
The following lines were added (+) and removed (-):
  ist1.clear 'Clear the list box  list1.clear 'Clear the list boxThe currently selected item's index can be returned with the listindex property. A listindex property of -1 indicates that no item has been selectedThe currently selected item's index can be returned with the listindex property. A listindex property of -1 indicates that no item has been selected. This can be used to set the selected item as well such as in the following example taken from [[Strange_Errors_in_VB6#Automation_error.2C_The_object_invoked_has_disconnected_from_its_clients.]] cboSkGroup.AddItem ("<All Skill Groups Available>") cboSkGroup.ListIndex = 0 === Style === You can make the combobox allow the user to enter something not in the drop down list, or be restricted to selecting only what is in the drop down list with the style property. * 0 fmStyleDropDownCombo - allow user to enter item not in list or select from list* 2 fmStyleDropDownList - user only allowed to select from listThe Visual BASIC 6.0 ComboBox does support multiple columns with no additional references necessary.   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 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 SubStudy the example above.  Note how the selected text changes.  Also note how what is displayed in the combo box changes.Next Example: '''Simulate the way a ComboBox works on a Web Form!''' (very useful)You want to select an employee name from the combo box, yet capture the hidden employee code which is the key field for doing a database query.  This is common on web forms.  You can do it in a Visual BASIC application also. 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.
Bureaucrat, administrator
16,192
edits