Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
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?
C#
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
Posted
Updated 20-Jul-10 2:55am
v2
Comments
Sandeep Mewara 20-Jul-10 8:55am    
Always use PRE tags to format code part. It makes the question readable.
Khaniya 20-Jul-10 10:06am    
Reason for my vote of 5
I have tried code and
I agree with him

You will have to create a array of strings and populate it. Then you can set this as lines for the textbox.

C#
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;
 
Share this answer
 
Comments
TheyCallMeMrJames 20-Jul-10 12:11pm    
ooooh...you got in just before me. I hate to brag, but mine's shorter....no, wait! I mean...
ahhashemi 20-Jul-10 23:19pm    
Reason for my vote of 5
I score it the best because he noticed what I was looking for and answered in the best way.
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();

}
 
Share this answer
 
Comments
dan!sh 20-Jul-10 12:51pm    
I didn't replied with LINQ expression since I don't know if OP is using .Net 3.5 or above.
TheyCallMeMrJames 20-Jul-10 13:00pm    
are you suggesting people aren't? hrm...maybe that explains the re-released tools to get people off of VB6...
ahhashemi 20-Jul-10 23:19pm    
Reason for my vote of 5
I score it the best because he noticed what I was looking for and answered in the best way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900