【发布时间】: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 选择的行与第一个组合框选择的值有关,所以它可能看起来像一个重复的问题,但它不是一回事
-
上一个问题是一种“全选”,而这个问题是“全选”