Reputation: 9081
How to insert a new line in textbox whithin winforms application :
textBox2.Text += "a";
textBox2.Text += "\n";
textBox2.Text += "b";
But the I get one row even I made the textbox multiline :
How can I fix this issue?
Upvotes: 0
Views: 1354
Reputation: 6849
for Windows OS you should write \r\n
for new line.
for Mac OS you should use \n
only.
The Environment.NewLine
will work for both operating system. Thus, you can make your code common for both platform. So, avoid \n
and \r\n
in the string when your application code is created for both operating system.
in your case you can use String.Format
to avoid multiple concatenation which will slow down the performance when it is being used in long process.
textBox2.Text = String.Format("ABCD{0}XYZ", Environment.NewLine);
if you are concatenating bunch of lines then there is better way to do this. You can use StringBuilder
class for that.
StringBuilder sb = new StringBuilder();
foreach(string line in Lines)
{
sb.AppendLine(line);
//or
//sb.AppendFormat("My line: {0}{1}", line, Environment.NewLine);
}
textBox2.Text = sb.ToString();
StringBuilder.AppendLine()
can be used also to add lines when you combining more than one lines.
Upvotes: 1
Reputation: 2042
As Brian said you need CRLF which means:
textBox2.Text += "a";
textBox2.Text += "\r\n";
textBox2.Text += "b";
But as Amir correctly said, Environment.NewLine
will do the trick as well and you don't have to bother with "how a newline is represented".
Upvotes: 0
Reputation: 29846
Use Environment.NewLine
textbox.Multiline = true;
textbox.Text += "a" + Environment.NewLine + "b";
Upvotes: 6