Lady Sour
Lady Sour

Reputation: 269

How to write multiple textboxes into a programme generated txt file in C#?

I have 4 textboxes and I have to write what's contained in there to a textfile I'll randomly generate. How will I write textbox data to the text file? and exactly where in the code? If i do it in :

private void textBox1_TextChanged(object sender, EventArgs e)

and have it all in the boxes textchanged parts, I can only get one textbox written.

Also how will I generate a txt file, that I'll randomly name with a 5 lettered name?

Upvotes: 1

Views: 2298

Answers (1)

Lloyd Powell
Lloyd Powell

Reputation: 18760

StreamWriter writeFile = new StreamWriter(fileLocation);
writeFile.WriteLine(textBox1.Text);
writeFile.WriteLine(textBox2.Text);
//etc.

writeFile.Close();

It depends what your aim is, as to when to save the file. If you want to save every time a text box text is changed, hook them all up to that event.

Althought if otherwise you could do it on a button click (save button?).

You will need to give more details for a more appropriate answer.

Upvotes: 2

Related Questions