News:

Choose a design and let our professionals help you build a successful website   - ITAcumens

Main Menu

text file to initialize a ComboBox connected with a ListBox

Started by nandagopal, May 20, 2009, 07:20 PM

Previous topic - Next topic

nandagopal

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



Quick Reply

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it has been approved by a moderator.

Name:
Email:
Verification:
Please leave this box empty:
Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

Shortcuts: ALT+S post or ALT+P preview