Home > Back-end >  Fill 2 combobox with items from txt file
Fill 2 combobox with items from txt file

Time:12-16

I have text file with this format

City A (Town A1, Town A2, Town A3)

City B (Town B1, Town B2)

City C (Town C1)

City D (Town D1, Town D2, TownD3, TownD4, TownD5)

I need to create two combobox. The first one you can choose with options of the city and then the second combobox will show the correct town. For example, when you chose City A in the first combobox, the second will show values like Town A1, Town A2, Town A3

So far I only have the fist combobox. How can i archive the second one? Here is my code. Thank you.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 Dim city As String
 Dim town As String
        For Each line As String In File.ReadAllLines("C:\Users\CP.txt")
            city = line.Substring(0, line.IndexOf(" "))
            cbbComunidad.Items.Add(city)
        Next line
    End Sub

CodePudding user response:

This looks like it's all string parsing.

Personally, I would probably take the substring to get the town values of each line between the parentheses first, then call split(",") against that to get each town (with comma as the separator). Then trim your towns. Store the result in a dictionary or similar. Here's some rough code to start with...

Dim dict As Dictionary(of String, List(Of String))
For Each line As String In File.ReadAllLines("C:\Users\CP.txt")
    Dim leftParenPos As Integer 
    leftParenPos = line.IndexOf("(")                        
    Dim city As String = line.Substring(0, leftParenPos-1).Trim()
    'Console.WriteLine(city)
    ' Get the towns from between the parentheses
    Dim townsString as String = line.Substring(leftParenPos 1, line.Length - leftParenPos - 2)
    'Console.WriteLine(townsString)         
    Dim towns As List(Of String)
    For Each town in townsString.Split(",")             
        towns.Add(town.Trim())
    Next town
    dict.Add(city, towns)
Next line

Now you have a dictionary you can use to look up the towns that go with the city. So you can add an event handler to your first combobox and reference the dictionary to get the towns.

CodePudding user response:

You can use some LINQ to extract the info. I've created a Form with 2 combo boxes. The cities are added on loading and the towns are added when a city is selected.

Public Class Form1   

    Private folderPath = "C:\Users\Public\Text Files"

    Private ReadOnly Property FileText As String()
        Get
            Return File.ReadAllLines(folderPath   "\CP.txt")
        End Get
    End Property

    Private Function GetCities() As List(Of String)
        Dim results =
            From line In FileText
            Let values = line.Split("(")
            Select values(0)
        Return results.ToList
    End Function

    Private Function GetTowns(city As String) As List(Of String)
        Dim results = From record In
            (From line In FileText
             Let values = line.Split("(")
             Select New With {
                    .CITY = values(0),
                    .TOWNS = values(1).Replace(")", "")
                })
                Where record.CITY.Trim = city.Trim
        Return results.FirstOrDefault.TOWNS.Split(", ").ToList
    End Function

    Private Sub cmbCities_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbCities.SelectedIndexChanged
        cmbTowns.DataSource = GetTowns(cmbCities.Text)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        cmbCities.DataSource = GetCities()
    End Sub

End Class
  • Related