0

コンマ区切りファイルから個別のデータを取得するコンボボックスがあります。その個別のデータを使用して、最初のコンボ ボックスに関する情報のみを含む 2 番目のコンボ ボックスを設定する必要があります。例: これがカンマ区切りのファイル テキストです。

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

そして、ここに私のコードがあります:

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

これは、各コンボボックスに個別のデータを入力するのにうまく機能しますが、2 番目のコンボボックスには、コンボボックス 1 で選択した項目からのデータのみを入力する必要があります。たとえば、「Cindy」を選択した場合、次のコンボボックスは、すべての個別の結果ではなく、25 と 75 のみを表示する必要があります。

4

1 に答える 1