Hey, i am making a program that will save some text from fields into a notepad, i have the save function worling perfectly but what i have no idea how to do is filling the fields with data from a saved notepad. I was planning to make a function that would search for a file (eg. account.txt) then it would load the data from that file into the fields on the form. Help! :@

Didn't quite get what you're trying to do.

But here's the basic idea, how you get data from a text line (VB2005 / .NET 2.0 and newer)

Dim FileText As String
Dim FileLines() As String
Dim OneLine() As String
Dim SeparatorChar As String
Dim i As Integer

' Check that file exists
If My.Computer.FileSystem.FileExists("D:\account.txt") Then
  ' Read the whole file to a string
  FileText = My.Computer.FileSystem.ReadAllText("D:\account.txt")
  ' Split to lines
  FileLines = FileText.Split(CChar(Environment.NewLine))
  ' Now FileLines array has all the lines from the file
  ' At this point you can use FileLines array directly

  ' If you want to separate a line, use this code
  ' Assume "," as the item separator in a line. Change this value if needed
  SeparatorChar = ","
  ' Loop the lines
  For i = 0 To FileLines.GetUpperBound(0)
    OneLine = FileLines(i).Split(CChar(SeparatorChar))
    ' Now OneLine array has a single line splitted
    ' Access line items from OneLine(0) to OneLine.GetUpperBound(0)
  Next i
End If

I tried to comment the code as well as possible. Use the parts of this snippet that you need.

HTH

Comments
Cool Approach

Sorry if i was't being clear with the problem, i can see what your getting at with the code but the SeperatorChar part is a bit confusing for me.
Basically i have three fields called txtAccount, txtUser and txt Pass.
They save into the notepad like so:
Account: Apple
Username: Orange
Password: Banana

basically i just want to fill my textboxes with the Apple, Orange and Banana part. I hope that makes sense, i'm currently trying the code you gave me so maybe it might click, but if there is something for my illness then could you post it? Thanks for the help! :icon_smile:

Account: Apple
Username: Orange
Password: Banana

basically i just want to fill my textboxes with the Apple, Orange and Banana part.

Basically, you did not run or test the code posted by Tame.

Because if you do, you will notice that he has 2 parts, parts 1 reading line by line, and part splitting the line into fields.

Dim FileText As String = "Account: Apple"
Dim FileLines As String()
Dim OneLine As String()
Dim Separator As String = ":"
FileLines = FileText.Split(CChar(Environment.NewLine))
OneLine = FileLines(0).Split(CChar(Separator))
Debug.Print(OneLine(1))

That is a demo of what you have.

Put the above in loop an and your problem is solved.

I hope Tame did not say he want 10$ because I use his approach ;)

I hope Tame did not say he want 10$ because I use his approach ;)

I normally charge 100$ per hour :D

I normally charge 100$ per hour :D

You are really VERY expensive to take 100$ for 7 line. :-O

I don't want to deal with you any more.

Tell me when you are offline so I can post my question. :D

You are really VERY expensive to take 100$ for 7 line. :-O

I was talking about charges I use in my day-time job :) Here in DaniWeb I post and help on the volunteer basis. I have been a newbie once and got a lot of help at that time. Now I have some experience and I really like to share my knowledge and help others. All without charging a dime ;)

Hey guys, i've been trying out the codes you posted and i think i know what some of its doing.
I can see that the filelines bit is spereating the file into lines then the seperator is looking for a char (in this case " : ") then seperating the line further.
What i've been trying to do is make it appear in my textboxes!
I'm not very experienced in VB so thats probably why most of this is new to me.
Something im not getting is what
Dim FileText As String = "Account: Apple" is for. If i have three different fields does that mean i make three different blocks of the code with different FileText strings? Im losing it here lol. The last thing i wanted to know was where do i put it? ive been putting it under Private Sub Fill() then teling the program to run it when the load button is clicked i dunno what im doing really =X

Something im not getting is what
Dim FileText As String = "Account: Apple" is for.

That was just a short-hand example representing a single line which you've read from your text file.

If i have three different fields does that mean i make three different blocks of the code with different FileText strings?

No. You have a single "block".

' Read the whole file to a string
FileText = My.Computer.FileSystem.ReadAllText("D:\account.txt")

After this line you have the whole file as a single string. That's why you do FileLines = FileText.Split(CChar(Environment.NewLine)) to get an array of the separate lines in the file.

Once you have an array of the lines, you loop them (a single "block")

For i = 0 To FileLines.GetUpperBound(0)
    OneLine = FileLines(i).Split(CChar(SeparatorChar))
    ' Now OneLine array has a single line splitted
    ' Access line items from OneLine(0) to OneLine.GetUpperBound(0)
  Next i

and in this loop you split a single line with the separator character. Now you have in OneLine an array of the parts of the current line.

Let's assume that you have a line (string) "Account: Apple" in FileLines(i) variable. When you split it OneLine = FileLines(i).Split(CChar(":")) , you'll have a two item array:
OneLine(0) contains "Account" and OneLine(1) contains " Apple".

To get those values to text boxes goes like this

TextBox1.Text = OneLine(0)
TextBox2.Text = OneLine(1)

Now TextBox1 should show "Account" and TextBox2 should show " Apple".

To get rid of spaces in the start and/or in the end of a string, use Trim method: TextBox2.Text = OneLine(1).Trim

ive been putting it under Private Sub Fill() then teling the program to run it when the load button is clicked

It's a good practice to put the code in a separate procedure. You'll get more readable code and it's easier to maintain. You're in the right track :)

Hi :)

Never mind from his code, it is lengthy and un-readable :icon_twisted: lol

Just kidding, it is good but I have this small snippet which may help you.

Dim _swFile As System.IO.StreamReader
Dim _Line, _Field As String
' Prepare the file to be read
_swFile = System.IO.File.OpenText("c:\xx.txt")
' Loop the file until end
While Not _swFile.EndOfStream
    ' Read a single line
    _Line = _swFile.ReadLine
    ' Split the line depending on ":"
    If Not IsDBNull(_Line.Split(":")(1)) Then
        _Field = _Line.Split(":")(1)
        Debug.Print(_Field)
    End If
End While

hth

' Read the whole file to a string
FileText = My.Computer.FileSystem.ReadAllText("D:\account.txt")

I just have one comment.

The above line will read the whole file at once. if the file is big it may lead to memory problem, I prefer to read line by line, I guess my code will be faster on PII with 64mb ram. :D

The above line will read the whole file at once. if the file is big it may lead to memory problem, I prefer to read line by line, I guess my code will be faster on PII with 64mb ram. :D

If you deal with small files (a few tens or hundreds of KBs) or you need to keep the whole file in the memory anyway, using My namespace is very handy requiring only a few lines of code. Besides, My namespace does have quite a few other handy classes too, not just My.Computer.FileSystem. If you don't need to keep the whole file in the memory or the file is really large, System.IO is absolutely the better solution. What comes to the speed of the code, with modern processor there's hardly any practically significant difference.

And after all, it's a question about selecting the right "tool" ;)

To get those values to text boxes goes like this

TextBox1.Text = OneLine(0)
TextBox2.Text = OneLine(1)

Now TextBox1 should show "Account" and TextBox2 should show " Apple".

That explanation totally cleared things up for me, thankyou so much! now my text box is showing text from the file but its showing the whole last line. This is what i have:

Private Sub Fill()
       
        
        If My.Computer.FileSystem.FileExists("C:\Travian.txt") Then

            FileText = My.Computer.FileSystem.ReadAllText("C:\Travian.txt")

            FileLines = FileText.Split(CChar(Environment.NewLine))
            SeparatorChar = ","

            For i = 0 To FileLines.GetUpperBound(0)
                OneLine = FileLines(i).Split(CChar(SeparatorChar))

            Next i
        End If

        txtUser.Text = OneLine(0)
    End Sub

After the button is clicked text shows up in txtUser but its the whole last line of my text document. Have i done something wrong here or have i given the wrong index for OneLine?

but its showing the whole last line.

Your last line does not have that separator character. That's why the whole line ends up to OneLine(0).

have i given the wrong index for OneLine?

It depends on your data, what the index should be. Start with zero and see what you get. Then try OneLine(1). You'll get an "Index out of bounds" error finally.

Can you tell me, for what purpose this

For i = 0 To FileLines.GetUpperBound(0)
   OneLine = FileLines(i).Split(CChar(SeparatorChar))
Next i

loop is? Do you need that loop? You have a single txtUser.Text text box (showing in your code). If you know that your data should come, for example, from the first line in the file, use simply

OneLine = FileLines(0).Split(CChar(SeparatorChar))
txtUser.Text = OneLine(0)

without a loop.

Or post the data you're using and explain how you should display it in the form.

Ok so my form looks like this

Account: _______
Username: ______
Password: ______
The account, username and password are labels and the ____ represent textboxes named appropriately. When i save the data it gets put into a text document that is named like this: txtAccount.Text & ".txt"
and the data in the text file is like this:

Account: Apple
Username: Orange
Password: Banana

basically what happens now is that it is only looking for one file (but ill fix it to look for the user specified file later) and then like you said it reads the file, splits it into lines then splits each line into two parts. I want the bit after the seperator to appear in the text box basically and so far its just putting the last line of the text file in. I also changed the value of OneLine to (1) but then it said the array was out of bounds so im thinking its actually starting to read from the last line of the text file.

Ok, you have three lines which you need to show (didn't remember your first post). Your code should be something like this

' Assuming you have multiple "triple" entries in your file
For i = 0 To FileLines.GetUpperBound(0) Step 3
  OneLine = FileLines(i).Split(CChar(SeparatorChar))
  txtAccount.Text = OneLine(1).Trim
  OneLine = FileLines(i + 1).Split(CChar(SeparatorChar))
  txtUserName.Text = OneLine(1).Trim
  OneLine = FileLines(i + 2).Split(CChar(SeparatorChar))
  txtPassword.Text = OneLine(1).Trim
Next i

' Or with a single entry in the file (no loop needed)
OneLine = FileLines(0).Split(CChar(SeparatorChar))
txtAccount.Text = OneLine(1).Trim
OneLine = FileLines(1).Split(CChar(SeparatorChar))
txtUserName.Text = OneLine(1).Trim
OneLine = FileLines(2).Split(CChar(SeparatorChar))
txtPassword.Text = OneLine(1).Trim

I also changed the value of OneLine to (1) but then it said the array was out of bounds

Check that SeparatorChar variable has the correct value (it should be ":" I think, now your code is using ",").

im thinking its actually starting to read from the last line of the text file.

It starts from the first line. Like I commented in the code above, it all depends if you have a single

Account: Apple
Username: Orange
Password: Banana

or multiple Account, Username and Password lines. In that case, the code will loop and use the last "triple".

YES!! IT WORKED! Oh thats fantastic i'm so happy. now i gotta figure out how to get the thing to look for files =P
Thankyou so much for the help and putting up with my crap!!!

Hi! Nice to hear that you got answer to your problem. Could you please mark the thread as solved. Thank you!

If you run in to a problem you can't solve, when looking for the files, start a new thread for that, please.

P.s. take a look at OpenFileDialog control...

This question has already been answered. Start a new discussion instead.