填充文本框对象 [英] Filling a TextBox Object

查看:81
本文介绍了填充文本框对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

在我的程序中,我需要从第二行到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();

}


这篇关于填充文本框对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆