|
|
Title | Use a text file to initialize a ComboBox connected with a ListBox |
Keywords | ComboBox, ListBox, initialize, load |
Categories | Controls, Tips and Tricks |
|
|
Load the data into a type defined to hold the data. In the ComboBox's Click event handler, populate the ListBox.
|
|
Private Type AuthorInfo
AuthorName As String
Titles As New Collection
Prices As New Collection
End Type
Private Authors(0 To 100) As AuthorInfo
Private MaxAuthor As Integer
Private Sub Form_Load()
Dim file As Integer
Dim last_author As String
Dim new_author As String
Dim new_title As String
Dim new_price As String
Dim i As Integer
file = FreeFile
Open App.Path & "\intext.txt" For Input As #file
MaxAuthor = -1
Do Until EOF(file)
Input #file, new_author, new_title, new_price
' If this author name is not the same
' as the last one, start a new author.
If new_author <> last_author Then
last_author = new_author
MaxAuthor = MaxAuthor + 1
Authors(MaxAuthor).AuthorName = new_author
' Add the name to the author combo.
cboAuthor.AddItem new_author
End If
' Add the new title and price.
Authors(MaxAuthor).Titles.Add new_title
Authors(MaxAuthor).Prices.Add new_price
Loop
Close #file
' Select the first author.
cboAuthor.ListIndex = 0
End Sub
Private Sub cboAuthor_Click()
Dim i As Integer
Dim num_titles As Integer
lstTitle.Clear
With Authors(cboAuthor.ListIndex)
num_titles = .Titles.Count
For i = 1 To num_titles
lstTitle.AddItem .Titles(i) & _
vbTab & .Prices(i)
Next i
End With
End Sub
|
|
|
|
|
|