Results 1 to 3 of 3

Thread: [RESOLVED] Read Line to Textbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    4

    Resolved [RESOLVED] Read Line to Textbox

    Basically what I want to achieve with this code is to read the text file and import the corresponding line to its textbox. But when i run this code BUT_A.text returns the value that is set for BUT_B on the textfile then any code after that doesn't import at all.

    Here's the code I am using for the open button (I'll be adding a OFD later)

    Code:
            Dim FileName As String = ("C:\options.ini")
            Dim FileReader As System.IO.StreamReader
    
            FileReader = System.IO.File.OpenText(FileName)
            FileReader.ReadLine()
            FileReader.ReadLine()
            If FileReader.ReadLine.ToString.StartsWith("BUT_A") Then txt_A.Text = FileReader.ReadLine()
            If FileReader.ReadLine.ToString.StartsWith("BUT_B") Then txt_B.Text = FileReader.ReadLine()
            If FileReader.ReadLine.ToString.StartsWith("BUT_C") Then txt_C.Text = FileReader.ReadLine()
    etc.


    Thanks for reading and I hope you can help!

    P.S. I also looked at the MSDN page for System.IO.StreamReader but I didn't really understand the meaning behind half of it. Can anyone link me to a indepth guide that explains all the terminologies and stuff?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Read Line to Textbox

    First things first, StreamReader.ReadLine returns a String, so there's no point calling ToString on the result.

    As for the issue, it's important to realise that EVERY time you call ReadLine, you are reading a line. How many times do you call ReadLine in this code:
    Code:
    If FileReader.ReadLine.ToString.StartsWith("BUT_A") Then txt_A.Text = FileReader.ReadLine()
    The answer is twice, therefore you are reading two lines. You are reading a line, checking whether it starts with a particular value and, if it does, you are reading another line and then displaying that second line in the TextBox. You then proceed to do that twice more, so you will be reading anything from three to six lines.

    What is it that you are actually trying to do? My guess is that you want to read every line and, for each line, test whether it starts with one of those values and, if it does, display it in the corresponding TextBox. If so the what you need to do is read the line into a variable and then use that variable each time you need that value, not keep reading more lines. Here's a start:
    vb.net Code:
    1. Using reader As New IO.StreamReader("file path here")
    2.     'Discard the first two lines.
    3.     For count = 1 To 2
    4.         reader.ReadLine()
    5.     Next
    6.  
    7.     Do Until reader.EndOfStream
    8.         Dim line = reader.ReadLine()
    9.  
    10.         'Use line here, e.g. check what it starts with and display it accordingly.
    11.     Loop
    12. End Using

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    4

    Re: Read Line to Textbox

    Ahh I see! That's much simpler than I thought it would have been.

    Thanks a bunch, just used the code and it worked like a charm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width