[英]c# How to read and write from multiline textBox line by line?
我有一个简单的程序,它有一个功能,当我按下一个按钮时,它可以从多行文本框中读取一行,我所做的就是这段代码:
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();
}
所以当点击 button1 它的代码将是这样的:
textBox2=text1[1];
[1] 表示第一行 我怎样才能通过单击自动完成? 或者单击第一行到 textBox2,第二行到 textBox3 .....等等。
请问我想要代码以及我应该把它放在哪里^_^
或者如果有另一种方法可以做到这一点
财产线是为你准备的
if(textBox1.Lines.Length > 0)
textBox2.Text=textBox1.Lines[0];
或者,将您的文本框排序在一个临时数组中并在它们上循环(当然我们应该始终检查 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];
}
简单的编程在C#中从多行文本框中一行一行地读写
一行一行地写:
textbox1.AppendText("11111111+");
textbox1.AppendText("\r\n222222222");
textbox1.AppendText("\r\n333333333");
textbox1.AppendText("\r\n444444444");
textbox1.AppendText("\r\n555555555");
逐行阅读:
for (int i = 0; i < textbox1.Lines.Length; i++)
{
textbox2.Text += textbox1.Lines[i] + "\r\n";
}
您可以使用以下代码段从多行文本框中读取逗号分隔和换行符分隔的值 -
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
}
}
我希望这会对你有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.