简体   繁体   English

如何在多个文本框中的文件行中将文本插入文本框?

[英]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. 我有一个包含9个TextBox控件的Form,以及一个包含9行文本的PlainText文件。

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? 我想点击一个按钮,然后将文本文件中的第一行文本添加到第一个TextBox中,然后将第二行添加到第二个文本框中,依此类推......任何人都可以提供有关如何执行此操作的任何建议?

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 编辑: 更改解决方案以使用File.OpenText而不是FileStream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM