【问题标题】:Populate a second combobox with data from the first combobox and a comma-delimited file使用来自第一个组合框和逗号分隔文件的数据填充第二个组合框
【发布时间】:2013-08-27 16:54:54
【问题描述】:

我有一个组合框,可以从逗号分隔的文件中获取不同的数据。 我需要使用该不同的数据并填充第二个组合框,其中仅包含有关第一个组合框的信息。 例子: 这是我用逗号分隔的文件文本。

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

这可以很好地将不同的数据填充到每个组合框中,但我希望第二个组合框仅填充来自组合框 1 中所选项目的数据,例如,如果我选择 “Cindy”下一个组合框必须只显示 25 和 75 而不是所有不同的结果

【问题讨论】:

  • @KenWhite 不,这个问题是不同的。它建立在你提到的他的问题上。这里nikki需要根据第一个选择的行绑定第二个组合框。
  • 我基本上需要填充第二个组合框,但 ownley 选择的行与第一个组合框选择的值有关,所以它可能看起来像一个重复的问题,但它不是一回事
  • 上一个问题是一种“全选”,而这个问题是“全选”

标签: vb.net combobox


【解决方案1】:

按照您的方式绑定第一个组合框。捕获组合框SelectedValueChanged 事件。 使用SelectedValue 属性,对文件进行第二次传递。这次将值添加到不同的哈希集中,如下所示:

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

你懂的

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

ComboBox2SelectedValueChanged写接受码

【讨论】:

  • 感谢您理解我的问题,您的回答很有效,谢谢您我所做的正如您所说的,它在 sub Dim 年龄 = New HashSet(Of String) Dim 名称的顶部工作得很好= New HashSet(Of String) 然后 currentRow = reader.ReadFields() names.Add(currentRow(0).ToString()) If currentRow(0).ToString() = ComboBox1.SelectedValue Thenages.Add(currentRow(1 ).ToString()) End If and at the bottom ComboBox2.DataSource =ages.ToList()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-11
  • 1970-01-01
相关资源
最近更新 更多