Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void button1_Click(object sender, EventArgs e)
        {
            string a;
            a = textBox1.Text;
            textBox2.Text += a;
            textBox1.Clear();
        }

this is what i have written the problem is that the code writes the next input in textbox1 next to the previous line but i want to write it in the next line...........how do I do it........please help..........




Regards,
Ahsan Naveed
Posted

Use this:

C#
private void button1_Click(object sender, EventArgs e)
{
    string a;
    a = textBox1.Text;
    textBox2.Text += String.Format("{0}{1}", (String.IsNullOrEmpty(textBox2.Text)) ? "" : Enviroment.NewLine, a);
    textBox1.Clear();
}


This will put a newline in front of the appended text from textbox1, but only if textbox2 already had some non-empty text inside it.

Regards,

Manfred
 
Share this answer
 
You can't, unless it is set as a multiline textbox (Set it's Multiline property to "true"). Then, it's just a case of adding a newline to the text:
C#
string a = textBox1.Text;
textBox2.Text += "\r\n" + a;
textBox1.Clear();
Or better:
C#
textBox2.Text = string.Format("{0}\r\n{1}", textBox2.Text, textBox1.Text);
textBox1.Clear();
Since it generates fewer intermediate strings.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 20-Aug-13 7:05am    
I forgot that part about setting the Multiline property to true. :doh:
Have a 5!

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