Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read file line by line and print to a text box c#

I am working on a windows forms application and am wanting to take a text file from my local machine and have the application read the text file and display each line of text from the file into a textbox on the application. I am wanting to press a button on the form and have the first line of the text file display, then press the button again and have the second line display etc.. I have been looking for ways to do this and have found that StreamReader will probably be best for what I am wanting to achieve.

I currently have the below code but it seems to print every line onto one line. If anybody can see why, it would be greatly appreciated, im sure that its something small.

private void btnOpen_Click(object sender, EventArgs e)
{
    string file_name = "G:\\project\\testFile.txt";
    string textLine = "";

    if (System.IO.File.Exists(file_name) == true)
    {
        System.IO.StreamReader objReader;
        objReader = new System.IO.StreamReader(file_name);

        do
        {
            textLine = textLine + objReader.ReadLine() + "\r\n";
        } while (objReader.Peek() != -1);

        objReader.Close();
    }
    else
    {
        MessageBox.Show("No such file " + file_name);
    }

    textBox1.Text = textLine;
}
like image 778
Richard Motion Avatar asked Dec 31 '25 01:12

Richard Motion


2 Answers

I would do it in a following way:

you are working with Windows Forms, so you have a Form class as your main class.

In this class I would define:

private string[] _fileLines;
private string _pathFile;
private int _index = 0;

and in constructor I would do

_fileLines = File.ReadAllLines(_pathFile);

and in button click event handler I would do:

textBox1.Text = _fileLines[_index++];
like image 195
Michał Turczyn Avatar answered Jan 01 '26 18:01

Michał Turczyn


Given

private string[] lines;
private int index =0;

Click Event

// fancy way of intializing the lines array
lines = lines ?? File.ReadAllLines("somePath");

// sanity check 
if(index < lines.Length)
   TextBox.Text = lines[index++]; // index++ increments after use

Additional Resources

File.ReadAllLines Method

Opens a text file, reads all lines of the file into a string array, and then closes the file.

?? Operator (C# Reference)

The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand.

++ Operator (C# Reference)

The unary increment operator ++ increments its operand by 1. It's supported in two forms: the postfix increment operator, x++, and the prefix increment operator, ++x.

Update

if I was to have the text file update with new lines constantly and I want to read one line after another with the button click, how would i go about that?

You can just use a local variable for lines, and just read the file every time

var lines = File.ReadAllLines("somePath");
if(index < lines.Length)
   TextBox.Text = lines[index++];
like image 31
TheGeneral Avatar answered Jan 01 '26 18:01

TheGeneral



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!