c#如何逐行读取和写入多行文本框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12957318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# How to read and write from multiline textBox line by line?
提问by Manar Al Saleh
I have a simple program it has a function to read a line from multiline textBox when i press a button what i made to do that is this code :
我有一个简单的程序,当我按下一个按钮时,它具有从多行文本框中读取一行的功能,这是我所做的代码:
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();
}
so when click button1 it the code will be like this:
所以当点击 button1 时,它的代码将是这样的:
textBox2=text1[1];
[1] mean the first line How can i do it automaticaly by one click ? or with one click the first line to textBox2 the second to textBox3 .....ect..
[1] 表示第一行 如何通过单击自动完成?或单击第一行到 textBox2 第二行到 textBox3 .....ect..
plz i want the code and where i should put it ^_^
plz我想要代码以及我应该把它放在哪里^_^
or if there is another way to do that
或者如果有另一种方法可以做到这一点
采纳答案by Steve
The property Linesis there for you
物业Lines为您服务
if(textBox1.Lines.Length > 0)
textBox2.Text=textBox1.Lines[0];
or, put your textboxes ordered in a temporary array and loop on them (of course checks the number of lines present in textBox1)
或者,将您的文本框排序在一个临时数组中并在它们上循环(当然检查 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];
}
回答by ShaileshDev
You can use following snippet for reading comma separated and newline separated values from multiline textbox -
您可以使用以下代码段从多行文本框中读取逗号分隔和换行符分隔的值 -
if (!string.IsNullOrEmpty(Convert.ToString(txtBoxId.Text)))
{
string IdOrder = Convert.ToString(txtBoxId.Text.Trim());
//replacing "enter" i.e. "\n" by ","
string temp = IdOrder.Replace("\r\n", ",");
string[] ArrIdOrders = Regex.Split(temp, ",");
for (int i = 0; i < ArrIdOrders.Length; i++)
{
//your code
}
}
I Hope this would help you.
我希望这会帮助你。
回答by Punit Poshiya
Simple programming read and write a one-by-one line from multiline textBox in C#
简单编程从C#中的多行textBox读写一行一行
Write line one-by-one:
一行一行写:
textbox1.AppendText("11111111+");
textbox1.AppendText("\r\n222222222");
textbox1.AppendText("\r\n333333333");
textbox1.AppendText("\r\n444444444");
textbox1.AppendText("\r\n555555555");
Read line one-by-one:
逐行阅读:
for (int i = 0; i < textbox1.Lines.Length; i++)
{
textbox2.Text += textbox1.Lines[i] + "\r\n";
}

