split comma seperated text into combo/text boxes VB.NET

Hi Experts,

Me again (sorry to be a pain)... I've asked for solutions on this before be each time with a slight twist)... I've gone through my history but cannot get it working!

I have a winform with:
Combobox1
Combobox2
Textbox1

All i want to do is select an item in Combobox... for this i use CASE SELECT... if (for example) I choose APPLES

I streamreads APPLES.txt into Combobox2 and splits the text between Combobox2 and Textbox1
e.g. APPLES.txt = "golden delicious, green"

Hope this helps - my head is wrecked & I need a quick solution.

Thanks,
Roberto

As you can see, I tried "Me.TextBox1.Text = line" and this displays "golden delicious, green" in Combobox2 & Textbox1

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim oRead As System.IO.StreamReader
        oRead = System.IO.File.OpenText("C:\apples.txt")
        While (oRead.Peek <> -1)
            Dim line As String = oRead.ReadLine
            Dim values As String() = line.Split(","c)
            Me.ComboBox2.Text = line
            Me.TextBox1.Text = line
        End While

    End Sub

Open in new window

RobertoFreemanoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
I *think* you're after this:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    Dim oRead As System.IO.StreamReader

    oRead = System.IO.File.OpenText("C:\apples.txt")

    While (oRead.Peek <> -1)
        Dim line As String = oRead.ReadLine
        Dim values As String() = line.Split(","c)

        Me.ComboBox2.Text = values(0)
        Me.TextBox1.Text = values(1)
    End While
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob LearnedCommented:
An easier approach would be to use the System.IO.File.ReadAllLines(path).

Public Shared Sub BindComboBoxToFileText(ByVal fileName As String, ByVal comboBox As ComboBox, ByVal displayMember As String, ByVal valueMember As String) 
     Dim fileText As String() = File.ReadAllLines(fileName)

     comboBox.Datasource = fileText
     comboBox.DisplayMember = displayMember
     comboBox.ValueMember = valueMember
End Sub

Open in new window

0
RobertoFreemanoAuthor Commented:
Hi kaufmed,
 That works great but can I show all the contents of my text file in combobox and text (based on items/list? (will extend points if need be).
e.g.
Combobox2 = "Golden delicious" / Textbox1 = "green"
Combobox2 = "Red delicious apples" / Textbox1 = "red"
Combobox2 = "Granny Smith apples" / Textbox1 = "green"
etc...

TEXTFILE: APPLES - this is what it looks like:
Golden delicious, green
Red delicious apples, red
Granny Smith apples, green

So if category (combobox1 is selected = APPLES) - it loads Apples list into Combobox2 and if item is selected, then show in Combobox2 & Textbox1

Thanks,
Roberto
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

RobertoFreemanoAuthor Commented:
TLO:

I get the following error :(
" 'ReadAllLines' is not a member of 'System.IO.File'."
Not sure why.
0
käµfm³d 👽Commented:
That works great but can I show all the contents of my text file in combobox and text (based on items/list?
That seems like an awful lot of I/O to be doing. Is there a specific need to read the file each time the SelectedIndexChanged event fires?
0
RobertoFreemanoAuthor Commented:
Yes.... maybe this will help explain better... forget APPLES (LOL) I was trying to simplify my request.

I need to load in a list of printers that exist on a server in work.... So the easy way to do so was to create text file named 'server1' which has a list of printers & IP Address if have collected.

e.g. HP LaserJet 5M,10.49.1.119

---example of what i'm trying to do----------------------------
Select Case ComboBox1.Text
            Case "server1"
ComboBox1.Text = " HP LaserJet 5M"
Textbox1.Text = "10.49.1.119"
        End Select

I have a button that pings the Textbox data (i.e. IP Address)

There are a lot of printers I have to access as my job is very repetitious!
0
RobertoFreemanoAuthor Commented:
oops

---example of what i'm trying to do----------------------------
Select Case ComboBox1.Text
            Case "server1"
ComboBox2.Text = " HP LaserJet 5M"
Textbox1.Text = "10.49.1.119"
        End Select
0
käµfm³d 👽Commented:
There are a lot of printers I have to access as my job is very repetitious!
My point is that can the file not be read on application start and stored in some data structure, and then that data structure will be consulted on index changed? The only business case I can think of to read file every time would be if there were the possibility of the file being updated during the course of application execution, and even then, I would probably design to accommodate this without reading the file every index change.
0
RobertoFreemanoAuthor Commented:
Thanks for your help kaufmed: ;)

& Thanks TLO ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.