Module: ListBox in Category Microsoft Visual Basic 6 : Controls from Total Visual SourceBook

Routines to extend the functionality of the standard Visual Basic 6 (VB6) ListBox control.

Some of these procedures expose features which are inherent in the Windows control, but are not exposed by VB6 (such as the ability to set add scrollbars or to set the height of items in the list). Other procedures add entirely new functionality such as loading a DAO recordset into an unbound list box.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the modListBox module.
AddItemDataListBox Procedure Adds an item to a list box, and the itemdata value associated with that item, in a single function call.
AddListBoxHSB Procedure Adds a horizontal scrollbar to a list box. VB6 does not allow you to specify that a horizontal scrollbar should appear for a list box. When items are too long to fit they are simply chopped off. By using a Windows API call you can specify that the logical width of the list box is different from the actual width. When the logical width is greater than the actual width, Windows automatically adds a horizontal scrollbar.
ADORecordSetToListBox Procedure Display the contents of an ADO recordset in a standard unbound list box. Create a recordset object and pass it to this procedure. Your recordset can be based on a table, query or SQL statement. You may designate one field in the recordset to display in the list box, and another field to store in the ItemData property. This lets you display a friendly field for the user to see, while storing an internal value like an ID number the user never sees.
Note: Use of the procedure requires you to have a reference to Microsoft ADO in your project.
BinarySearchListbox Procedure Use a binary search to find a value in a list box. A binary search works by dividing the values to be searched in half. If the search value is less than the value at the halfway point, then the top half is searched. If the value is greater than the value at the halfway point, the bottom half is searched. This process is repeated until the value is either found or no values remain to be searched. This technique requires the list box be sorted.
ClearSelectionsListBox Procedure Clears the selections for a multi-select list box. This function allows you to clear the selection from a multi-select list box with a single function instead of having to loop through each item separately.
CopyListBoxItemsToCB Procedure Copy selected items from a list box to the clipboard.
FillDrivesListBox Procedure Fills a list box with a list of the local and mapped disk drives on the system.
FillFilesListBox Procedure Fills a list box with a list of the files in a specified location.
FillFoldersListBox Procedure Fills a list box with a list of the folders in a specified location.
FindStringListBox Procedure Find the line on a list box containing the search string. This function uses the Windows API SendMessage command to quickly find a string in a standard list box control. You may search either for exact matches or partial matches on the beginning of the list box item. Note that case is not considered in doing the search, even if the fExact argument is true.
RecordSetToListBox Procedure Display the contents of a DAO recordset in a standard unbound list box. Create a recordset object and pass it to this procedure. Your recordset can be based on a table, query or SQL statement. You may designate one field in the recordset to display in the list box, and another field to store in the ItemData property. This lets you display a friendly field for the user to see, while storing an internal value like an ID number the user never sees.
Note: Use of the procedure requires you to have a reference to Microsoft DAO in your project.
SetListBoxItemHeight Procedure Set the height of items in a list box. VB6 normally uses the standard height for items in a listbox based on the type and size of the font in the list box. Using Windows API calls you can specify a different height.
SetListBoxTabStops Procedure Set tab stops for a list box. The standard VB6 list box uses the default tab stop positions. Using this procedure, you can create an array of tab stop positions to control the tab stops in a list box.
' Example of modListBox
'
' To try this example, do the following:
' 1. Create a new form
' 2. Create a list box named 'lstEmployee'
'    Set the following properties
'    Sorted      True
'    Multiselect 2 - Extended
'    Width       3500 twips
'    Height      2200 twips
' 3. Create a list box named 'lstDrives'
' 4. Create a list box named 'lstFolders'
' 5. Create a list box named 'lstFiles'
' 6. Create a command button 'cmdTest'
' 7. Create a command button 'cmdTestData'
' 8. Create a command button 'cmdRecordsetData'
' 9. Create a command button 'cmdADORecordsetData'
' 10. Paste all the code from this example to the new form's module.

' This example assumes that the sample files are located in the folder named by the following constant.
Private Const mcstrSamplePath As String = "C:\TVSBSamp"
Private Const mcstrConnect As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mcstrSamplePath & "\sample.mdb"

Private Sub cmdADORecordsetData_Click()
  ClearSelectionsListBox lstEmployee

  Call LoadADORecordsetData

End Sub

Private Sub Form_Load()
  Dim lngResult As Long
  Dim strWinDir As String

  cmdTest.Caption = "Test"
  cmdTestData.Caption = "Test Data"
  cmdRecordsetData.Caption = "Recordset Data"
  cmdADORecordsetData.Caption = "ADO Recordset Data"

  Call LoadSampleData

  ' Set the list box to be logically three times as wide as the actual width. Causes a horizontal scrollbar to appear.
  AddListBoxHSB lstEmployee, 3

  ' Double the current height of items in a list box
  SetListBoxItemHeight lstEmployee, 2

  ' Designate alternate tab stop positions
  SetListBoxTabStops lstEmployee, 70, 80, 100

  strWinDir = Environ("WINDIR")

  ' Load a listbox with the list of drives on the system
  lngResult = FillDrivesListBox(lstDrives)
  Debug.Print "Drives found: " & lngResult

  ' Load a list box with a list of folders in the user's "Windows" directory.
  ' (If the environment variable "WINDIR" is not set, this procedure will not work)
  lngResult = FillFoldersListBox(lstFolders, strWinDir & "\*.*")
  Debug.Print "Folders found: " & lngResult

  ' Load a list box with a list of files in the user's "Windows" directory.
  ' (If the environment variable "WINDIR" is not set, this procedure will not work)
  lngResult = FillFilesListBox(lstFiles, strWinDir & "\*.*")
  Debug.Print "Files found: " & lngResult

End Sub

Private Sub cmdTest_Click()
  Dim intLine As Integer

  ' Example of BinarySearchListBox
  intLine = BinarySearchListbox(lstEmployee, "Ellis")

  If intLine >= 0 Then
    MsgBox "'Ellis' Found on line: " & intLine
  Else
    MsgBox "'Ellis' Not Found"
  End If

  ' Example of FindStringListBox
  ' Find partial matches on search string 'Dave'
  intLine = FindStringListBox(lstEmployee, "Dave", False)

  If intLine >= 0 Then
    MsgBox "'Dave' Found on line: " & intLine
  Else
    MsgBox "'Dave' Not Found"
  End If

  ' Example of CopyListBoxItemsToCB Separate each item with a carriage return and a string of dashes. Copy selected items only
  CopyListBoxItemsToCB lstEmployee, True, vbCrLf & "---------------" & vbCrLf

End Sub

Private Sub cmdTestData_Click()
  ' Example of ClearSelectionsListBox

  ClearSelectionsListBox lstEmployee

  Call LoadSampleData

End Sub

Private Sub cmdRecordsetData_Click()
  ' Example of ClearSelectionsListBox

  ClearSelectionsListBox lstEmployee

  Call LoadRecordsetData

End Sub

Private Sub LoadSampleData()
  ' Example of AddItemDataListBox
  ' Simultaneously add a value for the list portion and the ItemData portion of a list box

  lstEmployee.Clear

  AddItemDataListBox lstEmployee, "Smith" & vbTab & "Bob" & vbTab & "I", 100
  AddItemDataListBox lstEmployee, "Nehru" & vbTab & "John" & vbTab & "R", 101
  AddItemDataListBox lstEmployee, "Proctor" & vbTab & "Bill" & vbTab & "Z", 102
  AddItemDataListBox lstEmployee, "Olsen" & vbTab & "Mary" & vbTab & "A", 103
  AddItemDataListBox lstEmployee, "Davenport" & vbTab & "Alan" & vbTab & "O", 104
  AddItemDataListBox lstEmployee, "Larue" & vbTab & "Lash" & vbTab & "B", 105
  AddItemDataListBox lstEmployee, "Paulsen" & vbTab & "Deana" & vbTab & "C", 106
  AddItemDataListBox lstEmployee, "Smithers" & vbTab & "Jim" & vbTab & "L", 107
  AddItemDataListBox lstEmployee, "Chung" & vbTab & "Axel" & vbTab & "R", 108
  AddItemDataListBox lstEmployee, "Haught" & vbTab & "Marv" & vbTab & "A", 109
  AddItemDataListBox lstEmployee, "Ferguson" & vbTab & "Thor" & vbTab & "U", 110
  AddItemDataListBox lstEmployee, "Ellis" & vbTab & "Petey" & vbTab & "D", 111
  AddItemDataListBox lstEmployee, "Juth" & vbTab & "Betty" & vbTab & "L", 112
  AddItemDataListBox lstEmployee, "Martin" & vbTab & "Louis" & vbTab & "O", 111
  AddItemDataListBox lstEmployee, "Levy" & vbTab & "Paddy" & vbTab & "T", 114
  AddItemDataListBox lstEmployee, "Clinton" & vbTab & "Ruth" & vbTab & "V", 115
  AddItemDataListBox lstEmployee, "Belzer" & vbTab & "Bill" & vbTab & "N", 116

End Sub

Private Sub LoadRecordsetData()
  ' Example of RecordSetToListBox

  Dim dbs As DAO.Database
  Dim rst As DAO.Recordset
  Dim strSQL As String

  strSQL = "SELECT ProductID, ProductName " & _
           "FROM Products " & _
           "ORDER BY ProductName "

  Set dbs = DAO.DBEngine(0).OpenDatabase(mcstrSamplePath & "\sample.mdb")

  Set rst = dbs.OpenRecordset(strSQL, dbOpenSnapshot)

  Screen.MousePointer = vbHourglass
  RecordSetToListBox rst, lstEmployee, "ProductName", "ProductID"
  Screen.MousePointer = vbDefault

End Sub

Private Sub LoadADORecordsetData()
  ' Example of ADORecordSetToListBox

  Dim rst As ADODB.Recordset
  Dim strSQL As String

  strSQL = "SELECT ProductID, ProductName " & _
           "FROM Products " & _
           "ORDER BY ProductName "

  Set rst = New ADODB.Recordset
  rst.Open strSQL, mcstrConnect

  Screen.MousePointer = vbHourglass

  lstEmployee.Clear

  ADORecordSetToListBox rst, lstEmployee, "ProductName", "ProductID"
  Screen.MousePointer = vbDefault

End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS