c#如何逐行读取和写入多行文本框?

Man*_*leh 15 c# string text textbox line

我有一个简单的程序,它有一个函数从多行textBox读取一行当我按下按钮我做的是这个代码:

TextReader read = new System.IO.StringReader(textBox1.Text);
int rows = 100;

string[] text1 = new string[rows];
for (int r = 1; r < rows; r++)
{
    text1[r] = read.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)

所以当点击button1时,代码将是这样的:

textBox2=text1[1];
Run Code Online (Sandbox Code Playgroud)

[1]意味着第一行如何通过一次点击自动完成?或者单击第一行到textBox2第二行到textBox3 .....等.

PLZ我想要代码和我应该把它放在哪里^ _ ^

或者如果有另一种方法可以做到这一点

Ste*_*eve 21

物业线为您服务

if(textBox1.Lines.Length > 0)
    textBox2.Text=textBox1.Lines[0]; 
Run Code Online (Sandbox Code Playgroud)

或者,将您的文本框排序在一个临时数组中并循环它们(当然检查textBox1中的行数)

TextBox[] text = new TextBox[] {textBox2, textBox3, textBox4};
if(textBox.Lines.Length >= 3)
{
    for(int x = 0; x < 3; x++) 
       text[x] = textBox1.Lines[x];
}
Run Code Online (Sandbox Code Playgroud)

  • 注意:Textbox.lines仅适用于Windows窗体(不适用于Web窗体) (2认同)

小智 8

简单的编程从C#中的多行textBox读取和写入一行一行

逐行写入:

textbox1.AppendText("11111111+");
textbox1.AppendText("\r\n222222222");
textbox1.AppendText("\r\n333333333");
textbox1.AppendText("\r\n444444444");
textbox1.AppendText("\r\n555555555");
Run Code Online (Sandbox Code Playgroud)

逐行阅读:

for (int i = 0; i < textbox1.Lines.Length; i++)
{
    textbox2.Text += textbox1.Lines[i] + "\r\n";
}
Run Code Online (Sandbox Code Playgroud)