填充文本框对象
问题描述:
你好,
在我的程序中,我需要从第二行到richTextBox控件的末尾填充一个TextBox对象.我使用了下面的代码,但我想知道为什么它不起作用.您能告诉我如何使它工作吗?
Hello,
In my program I need to fill a TextBox Object from the second line to the end of a richTextBox Control. I used the code below but I wonder why it does not work. Could you please let me know how could I make it work?
private void button1_Click(object sender, EventArgs e)
{
int intLng = richTextBox1.Lines.Length;
TextBox MyTextBox = new TextBox();
MyTextBox.Lines = new String[intLng - 1];
MyTextBox.Multiline = true;
for ( int i =1 ; i < intLng ; i ++ )
{
MyTextBox.Lines[i - 1] =richTextBox1.Lines[i].ToString();
}
}
非常感谢
Thank you a lot
答
您将不得不创建一个字符串数组并填充它.然后,您可以将其设置为文本框的行.
You will have to create a array of strings and populate it. Then you can set this as lines for the textbox.
int intLng = richTextBox1.Lines.Length;
TextBox MyTextBox = new TextBox();
MyTextBox.Lines = new String[intLng - 1];
MyTextBox.Multiline = true;
string[] str = new string[intLng - 1];
for (int i = 1; i < intLng; i++)
{
str[i - 1] = richTextBox1.Lines[i].ToString();
}
MyTextBox.Lines = str;
它是公共的,但是您必须传递初始化数组.
糟透了,对不起,但这是它的工作方式.
LINQ来救援!
It''s public, but you have to pass in the initialized array.
Sucks, sorry, but it''s how it works.
LINQ to the rescue!
private void button1_Click(object sender, EventArgs e)
{
TextBox MyTextBox = new TextBox();
MyTextBox.Multiline = true;
MyTextBox.Lines = richTextBox1.Lines.Skip(1).ToArray();
}