jay_t55
jay_t55

Reputation: 11662

How do I insert text into textbox from line in file in multiple textboxes?

I am trying to do something but I haven't found anything on google since I don't know how to word it to get the right results.

I have a Form with 9 TextBox controls, and a PlainText file with 9 lines of text.

I want to click a button which will then add the first line of text from the text file into the first TextBox, then the second line into the second textbox, and so on... Can anybody please provide any advice on how to do so?

Upvotes: 3

Views: 2825

Answers (1)

Donut
Donut

Reputation: 112895

Try this:

using (StreamReader reader = File.OpenText("yourFileName.txt"))
{
    textBox1.Text = reader.ReadLine();
    textBox2.Text = reader.ReadLine();
    textBox3.Text = reader.ReadLine();
    textBox4.Text = reader.ReadLine();
    textBox5.Text = reader.ReadLine();
    textBox6.Text = reader.ReadLine();
    textBox7.Text = reader.ReadLine();
    textBox8.Text = reader.ReadLine();
    textBox9.Text = reader.ReadLine();
}

edit: changed solution to use File.OpenText instead of FileStream

Upvotes: 9

Related Questions