Hi all

I am trying to get my program to read a text file one line at a time and then return the first line to text box 1, the second line to text box 2 and so on. It is a recipe storing program with a list of ingredients. I can write to the text file no problem but reading it is proving difficult. I ahve attached my code so far:

Public Class Form3

Private Sub cmdView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdView.Click
    Dim FILE_NAME As String = "C:\Users\wm\test2.txt"

    Dim TextLine As String

    If System.IO.File.Exists(FILE_NAME) = True Then

        Dim objReader As New System.IO.StreamReader(FILE_NAME)

        Do While objReader.Peek() <> -1

            TextLine = TextLine & objReader.ReadLine() & vbNewLine


        TextBox1.Text = TextLine


        MsgBox("File does not exist")
    End If
End Sub

End Class

Recommended Answers

All 9 Replies

In your Do While loop you are reading the file until the end.
Then the whole of the file is assigned to TextBox1.Text.

Hi thanks for the response. I knwo this already, that is what I dont know how to change! I know its probably really easy but I just cant get it!

Change line 12 to TextLine = objReader.ReadLine() & vbNewLine
You where reading the file line by line and then concatenating these lines.
Make an array of your textboxes and put them in your loop
Something like: TextBox(i).Text = line read from file

You could just create the controls at runtime and put all of them inside a panel as in

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    Dim xpos As Integer = 10
    Dim ypos As Integer = 20


    For Each line As String In System.IO.File.ReadAllLines("d:\temp\test.txt")
        Dim txt As New TextBox
        txt.Text = line
        txt.Location = New Point(xpos, ypos)
        ypos += 25

End Sub

As long as you set the AutoScroll property of the panel to True you'll be able to scroll through the ingredients and you won't have to worry about not having enough controls for the ingredients. You can use AddHandler to add an event handler to the newly creaded controls.

On the other hand, why not just use a multi-line textbox and display the ingredients as

TextBox1.Text = System.IO.File.ReadAllText("d:\temp\test.txt")
commented: Nice point of view. +15

Hi thanks for the replies. I have used the multi line textbox but I need to keep them seperate. I need to take the values of each text box and do calculations with the quantities of the ingrediants i.e. change the number of people for the recipe and then allow it to work out the new quantites.

This is why I figured I need them in individual text boxes as then I can run some calcultions on the values.

You might also consider a ListBox or even a ComboBox

But would these allow me to display results from the text file? I need each line of the text file displayed in individual boxes. Would this allow for this?

Because you have a list of ingredients I would prefer a listbox here. You can iterate over every element, just like you would do with your textboxes.
Besides: you have recepies with two ingredients and recepies with say 20 ingrdients. How you are going to handle that with textboxes? A listbox can easily take 100 items and more.

I think you want the selected line text and else line want to ignore...
If I am correct then you can do this:
I will give example with 1st line and last line of text...
I think you will get some idea from this:
Read First And Last Line:
button 1 codes (open)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub

Open File Dialog 1 code

Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
            Dim lines() As String = IO.File.ReadAllLines(OpenFileDialog1.FileName)
            TextBox1.Text = lines(0)
            TextBox2.Text = lines(lines.Length - 1)
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.