Question Parsing text and sending it to multiple comboboxes

kylegee

Member
Joined
Sep 9, 2008
Messages
10
Programming Experience
Beginner
So basically I have a CSV file with 30 elements per line. I can separate the elements fine with the use of Split(",").

Now I need to make a loop that will send the text from each element to a corresponding, unique, combobox.



The bolded area is where my problem is, because you can't handle an object that way...

VB.NET:
Dim FileHolder As FileInfo = New FileInfo("C:\bob.pro")

Dim TxtRDR As StreamReader = FileHolder.OpenText

Dim j As Integer = 2

Do

Dim Data = TxtRDR.ReadLine.Split(",")

For j As Integer = 1 To Data.Length

[SIZE="4"][B]ComboBoxj.text = Data(j)[/B][/SIZE]

Next

Loop Until TxtRDR.ReadLine Is Nothing



Is there any easy way for me to accomplish what I need?
 
You probably have a warning on this line :

VB.NET:
Dim Data = TxtRDR.ReadLine.Split(",")

I know .NET 3.5 has some new features like type inference, so I could be wrong saying this, but I think it doesn't apply in this case and replacing that line by the following should solve the problem.

VB.NET:
Dim Data as String() = TxtRDR.ReadLine.Split(",")

This will prevent Data from having the default Object type.

EDIT : Some googling showed that type inference must be activated in the project for the type to be automatically inferred from the assignation. It's really your choice as to which you like...
 
No, there isn't a warning.

This script works, but I wish I could find a solution to my loop problem.

VB.NET:
    Private Sub LoadPreAct()

        Dim FileHolder As FileInfo = New FileInfo("C:\bob.pro")
        Dim TxtRDR As StreamReader = FileHolder.OpenText

        Dim Data = TxtRDR.ReadLine.Split(",")
        ComboBox1.Text = Data(2)
        ComboBox2.Text = Data(3)
        ComboBox3.Text = Data(4)
        ComboBox4.Text = Data(5)
        ComboBox5.Text = Data(6)
        ComboBox6.Text = Data(7)
        ComboBox7.Text = Data(8)
        ComboBox8.Text = Data(9)
        ComboBox9.Text = Data(10)
        ComboBox10.Text = Data(11)
        ComboBox11.Text = Data(12)
        ComboBox12.Text = Data(13)
        ComboBox13.Text = Data(14)
        ComboBox14.Text = Data(15)
        ComboBox15.Text = Data(16)
        ComboBox16.Text = Data(17)
        ComboBox17.Text = Data(18)
        ComboBox18.Text = Data(19)
        ComboBox19.Text = Data(20)
        ComboBox20.Text = Data(21)
        ComboBox21.Text = Data(22)
        ComboBox22.Text = Data(23)
        ComboBox23.Text = Data(24)
        ComboBox24.Text = Data(25)
        ComboBox25.Text = Data(26)
        ComboBox26.Text = Data(27)
        ComboBox27.Text = Data(28)
        ComboBox28.Text = Data(29)
        ComboBox29.Text = Data(30)
        ComboBox30.Text = Data(31)
    End Sub

I have inference active by default, its handy when dealing with LINQ.
 
Oh, alright, now I understand your problem... There are two solutions I can think of for you.

You could create an array to contain the comboboxes, then iterate over the array. The array could be a class member so you could use it at other locations.

Or you could do a search by name on the container of the comboboxes. This will only work if you have put all the comboboxes on the same panel (or directly on the form). Do something like this :

VB.NET:
for i as integer = 1 to 30
    ' I do think you can retrieve a Control by its name like this, otherwise you'll have to look for it in a loop.
    this.Controls.Item("ComboBox" + i.ToString()).Text = Data(i + 1)
next

This is pretty rough as I have been doing C# for a while, but it should work. Just correct any typo and replace "this.Controls" by the container of the comboboxes if they are inside a panel.
 

Latest posts

Back
Top