locked
combox with text from txtfile RRS feed

  • Question

  • I have a code for populate a combobox with the text from a txtfile.

     

     For Each line As String In IO.File.ReadAllLines("Test.txt")

                Combobox1.Items.Add(line)

                Combobox1.Text = (" ")

     

            Next

     

    But i have about 20 comboboxen en i want  all of them with this text from the txtfile.

     

    (Every Combobox in the Form)

    How do i do that ?

    Wednesday, February 1, 2012 3:12 PM

Answers

  • I guess the easiest way would be to first store the list strings in a collection and then use that collection to fill the items in each of you comboboxes

            Dim ListValues As New List(Of String)
    
            For Each line As String In IO.File.ReadAllLines("Test.txt")
                ListValues.Add(line)
            Next
    
            ComboBox1.Items.AddRange(ListValues.ToArray)
            ComboBox2.Items.AddRange(ListValues.ToArray)
            ComboBox3.Items.AddRange(ListValues.ToArray)
    
    

    If you have 20 comboboxes then you could store references to them in a collection and loop through the collection to perform the AddRage call above.  You could also use a recursive function to find all the comboboxes on the form and fill them that way

       Public Sub GetComboData()
    
            Dim ListValues As New List(Of String)
    
            For Each line As String In IO.File.ReadAllLines("Test.txt")
                ListValues.Add(line)
            Next
    
            FillCombos(Me, ListValues)
    
        End Sub
    
        Private Sub FillCombos(ctrl As Control, ListValues As List(Of String))
    
            If TypeOf ctrl Is ComboBox Then
                CType(ctrl, ComboBox).Items.AddRange(ListValues.ToArray)
            End If
    
            For Each c As Control In ctrl.Controls
                FillCombos(c, ListValues)
            Next
    
        End Sub
    

    You would call GetCOmpboData from, say, your form's load event.

     

    • Proposed as answer by Cor Ligthert Wednesday, February 1, 2012 4:11 PM
    • Marked as answer by KeesBlunder Wednesday, February 1, 2012 6:19 PM
    Wednesday, February 1, 2012 3:28 PM

All replies

  • I guess the easiest way would be to first store the list strings in a collection and then use that collection to fill the items in each of you comboboxes

            Dim ListValues As New List(Of String)
    
            For Each line As String In IO.File.ReadAllLines("Test.txt")
                ListValues.Add(line)
            Next
    
            ComboBox1.Items.AddRange(ListValues.ToArray)
            ComboBox2.Items.AddRange(ListValues.ToArray)
            ComboBox3.Items.AddRange(ListValues.ToArray)
    
    

    If you have 20 comboboxes then you could store references to them in a collection and loop through the collection to perform the AddRage call above.  You could also use a recursive function to find all the comboboxes on the form and fill them that way

       Public Sub GetComboData()
    
            Dim ListValues As New List(Of String)
    
            For Each line As String In IO.File.ReadAllLines("Test.txt")
                ListValues.Add(line)
            Next
    
            FillCombos(Me, ListValues)
    
        End Sub
    
        Private Sub FillCombos(ctrl As Control, ListValues As List(Of String))
    
            If TypeOf ctrl Is ComboBox Then
                CType(ctrl, ComboBox).Items.AddRange(ListValues.ToArray)
            End If
    
            For Each c As Control In ctrl.Controls
                FillCombos(c, ListValues)
            Next
    
        End Sub
    

    You would call GetCOmpboData from, say, your form's load event.

     

    • Proposed as answer by Cor Ligthert Wednesday, February 1, 2012 4:11 PM
    • Marked as answer by KeesBlunder Wednesday, February 1, 2012 6:19 PM
    Wednesday, February 1, 2012 3:28 PM
  • Very nice , thank you !

     

    Save's a lot of work en code.

    Wednesday, February 1, 2012 6:20 PM