2013-08-27 2 views
0

쉼표로 구분 된 파일에서 별개의 데이터를 가져 오는 콤보 상자가 있습니다. 그 별개의 데이터를 사용하고 첫 번째 콤보 상자에 대한 정보 만 포함하는 두 번째 콤보 상자를 채워야합니다. 예 : 여기에 쉼표로 구분 된 파일 텍스트가 있습니다.첫 번째 콤보 상자와 쉼표로 구분 된 파일의 데이터로 두 번째 콤보 상자 채우기

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 예를 선택한 항목의 데이터를 채우려면 두 번째 콤보 상자를 원한다 내가 선택한 "신디"다음 콤보 박스는 25와 75 만 보여야 만한다.

+0

중복 가능성에 아이디어를

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

쓰기 승인 코드를 얻을 데이터베이스 대신 쉼표로 구분 된 파일에서?] (http://stackoverflow.com/questions/18468627/visual-basic-how-to-add-a-destinct-combobox-from-a-comma-delimited-file -instead) –

+0

@KenWhite 아니오,이 질문은 다릅니다. 당신이 언급하는 질문에 대한 답을 얻습니다. 여기서 nikki는 첫 번째 선택된 행을 기반으로 두 번째 콤보 박스를 바인딩해야합니다. – inquisitive

+0

기본적으로 두 번째 콤보 상자를 채울 필요가 있지만 첫 번째 콤보 상자를 선택한 값과 관련하여 행을 선택하면 중복 질문처럼 보일 수 있지만 일치하지는 않습니다. – Nikki

답변

2

첫 번째 콤보 박스를 당신이하는 방식대로 묶어 라. 콤보 박스 SelectedValueChanged 이벤트를 잡아라. SelectedValue 속성을 사용하여 파일을 두 번째 패스합니다. 이 시간은 다음과 같이 서로 다른 HashSet의 값을 추가 :

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 

당신이 Destinct 콤보 상자를 추가하는 방법 [Visual Basic에서의 ComboBox2SelectedValueChanged

+0

내 질문을 이해하기위한 고마워요, 당신의 대답은 위대한 일을했는데, 내가 말한대로 다음과 같은 일을 잘 했어. 서브의 맨 위에서 잘해라. Dim ages = New HashSet (Of String) Dim names = New HashSet (Of String) 및 currentRow = reader.ReadFields() names.Add (currentRow (0) .ToString()) currentRow (0) .ToString() = ComboBox1.SelectedValue 그런 다음 ages.Add (currentRow (1). ToString()) End If 및 아래쪽 ComboBox2.DataSource = ages.ToList() – Nikki

+0

@Nikki, 잘 알고있는 것이 좋았습니다. – inquisitive

관련 문제