Hi all,

I am new here and to programming and i am currently trying to build an interactive form for my works. I have my form which at the moment saves text from textbox1 to a .txt file and also i have succeeded in being able to read this back into a textbox of my choice.

My probelm is that i want "line 1" from the text file to go to one textbox and "line 2" to go to another. I have used StreamWriter and StreamReader and i have managed to read back "line 1" but i cannot figure out how to tell it to read "line 2".

My code at present is as follows:

using System;
using System.IO;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Creating a new stream-writer and opening the file.
            System.IO.StreamWriter StreamWriter1 = new System.IO.StreamWriter(@"C:\Test.txt");
           
            //Writing text to the file.
            StreamWriter1.Write(textBox1.Text);

            //Close the file.
            StreamWriter1.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Creating a new stream-reader and opening the file.
            System.IO.StreamReader StreamReader1 = new System.IO.StreamReader(@"C:\Test.txt");

            //Reading Line from file
            String text = StreamReader1.ReadLine();

            //Writing the read line to textBox
            textBox3.Text = text;

            //Close the file
            StreamReader1.Close();
        }
    }
}

I have searched through the forums already, which were really helpfull but I'm stuck here.
Hope i have provided enough information.

Thanks in advance

Michael

Recommended Answers

All 3 Replies

I managed to work out my problem after a lot of searching. My new Code for "button 2" looks like this:

private void button2_Click(object sender, EventArgs e)
        {
            //Creating a new stream-reader and opening the file.
            System.IO.StreamReader StreamReader1 = new System.IO.StreamReader(@"C:\Test.txt");
            
            //Reading Line from file
            string newLine;
            while ((newLine = StreamReader1.ReadLine()) != null)
            

            //Writing the read line to textBox
            textBox3.AppendText(newLine);

            //Close the file
            StreamReader1.Close();
            }
        }
    }

Each time you use the ReadLine method, it will move the poistion marker in the stream to the next line. so you can keep using the same method to populate the other text boxes.

textBox1.Text = streamreader1.ReadLine();
textBox2.Text = streamreader1.ReadLine();
textBox3.Text = streamreader1.ReadLine();
// and so on.

Be a part of the DaniWeb community

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