简体   繁体   English

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

[英]c# How to read and write from multiline textBox line by line?

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? [1] 表示第一行 我怎样才能通过单击自动完成? or with one click the first line to textBox2 the second to textBox3.....ect..或者单击第一行到 textBox2,第二行到 textBox3 .....等等。

plz i want the code and where i should put it ^_^请问我想要代码以及我应该把它放在哪里^_^

or if there is another way to do that或者如果有另一种方法可以做到这一点

The property Lines is there for you财产线是为你准备的

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 we should always check 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];
}

Simple programming read and write a one-by-one line from multiline textBox in C#简单的编程在C#中从多行文本框中一行一行地读写

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";
}

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.我希望这会对你有所帮助。

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

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