vb.net How upload data from a .txt file into the controls on the UI using control name

Here is am example of my .txt file:


<%:ProteinName="CP4 EPSPS"\%>

please ignore <%:\%>

I want to be able to recursively add the data back to the textboxes and comboboxes using this format.
Natavia FinnieSenior Analyst Technical Focus ApplicationsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Fernando SotoRetiredCommented:
Hi taviaf;

Read each line of the file and parse it to control name and the value. Then on the Form that the control is on you can do the following.

Dim ctl = Me.Controls.Find(name, True)

Open in new window

Where the name is the control you need to update and True is to search ALL child controls. That method returns an array of controls. If the control was found it will be in position 0. Depending on which property you may be updating you may need to cast to the control type. For example

Dim tb As TextBox = DirectCast(ctl(0), TextBox)

Open in new window

In this sample you can update the TextBox, tb, as needed.
Natavia FinnieSenior Analyst Technical Focus ApplicationsAuthor Commented:
@Fernando Soto, I am new to vb.net so you may have to be just a bit more detailed.  Sorry....
Fernando SotoRetiredCommented:
When you say that to, please ignore <%:\%>, does that mean that it is not in the file?
Natavia FinnieSenior Analyst Technical Focus ApplicationsAuthor Commented:
No that is in the file. If I was to use part of the current function it ignores those. Put if I was to write my own then DO NOT ignore.

In this case do not ignore because the .txt file is saved like the example that I gave.  

That's all.....
Fernando SotoRetiredCommented:
Hi taviaf;

The below code should do what you need or at least get you going.

Imports System.IO

'' Load the data from the file into memory
Dim fileData As String() = File.ReadAllLines("C:\Working Directory\MSDN Forum\WindowsApplication4\WindowsApplication4\bin\Debug\ctlData.txt")

'' Iterate through all the lines in the file and load the data into the controls
For Each nameAndData As String In fileData
    '' Trim off all the characters from the front and end of the string that are not needed
    '' and the double quotes around the values
    Dim name = nameAndData.Trim(New Char() {"<"c, "%"c, ":"c, "\"c, ">"c}).Replace("""", "").Split(New Char() {"="c})
    '' Get a reference to the control with the name
    Dim ctl As Control() = Me.Controls.Find(name(0), True)
    '' Get the control type name, TextBox or ComboBox
    Dim ctlType As String = ctl(0).GetType().Name
    '' See if it is one of the control type to update
    If ctlType = "TextBox" OrElse ctlType = "ComboBox" Then
        '' Update the TextBox
        If ctlType = "TextBox" Then
            Dim tb As TextBox = DirectCast(ctl(0), TextBox)
            tb.Text = name(1)
            '' Update the ComboBox
            Dim cb As ComboBox = DirectCast(ctl(0), ComboBox)
        End If
    End If

Open in new window

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
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.