Nikki
Nikki

Reputation: 85

Populate a second combobox with data from the first combobox and a comma-delimited file

I have a combobox that gets distinct data from a comma-delimited file. I need to use that distinct data and populate a second combobox containing only information about the first combo box. Example: Here's my comma-delimited files text.

Jenny, 25, Female
Micheal, 100, Female, hdfgh
shaun, 50, male
Cindy, 75, Female
Jenny, 25, Female
Micheal, 100, Female
shaun, 50, male
Cindy, 50, Female
Carry, 75, Female

And here's my code:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Call combo1()
    Call combo2()
End Sub

Sub combo1()
    ' a set to store the names
    Dim names = New HashSet(Of String)
    Using reader = New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\here.txt")

        reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
        reader.Delimiters = New String() {","}
        Dim currentRow As String()
        While Not reader.EndOfData
            Try
                ' read rows and add first field to set
                currentRow = reader.ReadFields()
                names.Add(currentRow(0).ToString())
            Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                ' do something
            End Try
        End While
    End Using

    ' bind data to combo box (or use Items.Add instead)
    ComboBox1.DataSource = names.ToList()
End Sub

Sub combo2()
    ' a set to store the names
    Dim names = New HashSet(Of String)
    Using reader = New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\here.txt")

        reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
        reader.Delimiters = New String() {","}
        Dim currentRow As String()
        While Not reader.EndOfData
            Try
                ' read rows and add first field to set
                currentRow = reader.ReadFields()
                names.Add(currentRow(1).ToString())
            Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                ' do something
            End Try
        End While
    End Using

    ' bind data to combo box (or use Items.Add instead)
    ComboBox2.DataSource = names.ToList()
End Sub

This works fine to populate distinct data into each combobox, but I want the second combobox to only populate data from the selected item in combobox 1, example if I selected "Cindy" the next combobox must only show 25 and 75 not all the distinct resauls

Upvotes: 0

Views: 1135

Answers (1)

inquisitive
inquisitive

Reputation: 3639

bind the first combobox the way you are doing. trap the combobox SelectedValueChanged event. use theSelectedValue property, do a second pass on the file. This time add values to the different hashset like this:

Dim ages = New HashSet(Of String)
    Using reader = New Microsoft.Visua...   ...FieldParser("C:\here.txt")

        reader.TextFieldType = Micros...   ....eldType.Delimited
        reader.Delimiters = New String() {","}
        Dim currentRow As String()
        While Not reader.EndOfData
            Try
                '   v--- THIS LINE IS IMPORTANT ---v
                if currentRow(0).ToString() = combobox1.selectedvalue then

                    ages.Add(currentRow(1).ToString())
                end if

you get the idea

    ' bind data to combo box (or use Items.Add instead)
    ComboBox2.DataSource = ages.ToList()

write acceptance code in ComboBox2 SelectedValueChanged

Upvotes: 2

Related Questions