Help on help... And stringing things along...

  • Thread starter Thread starter Chuck Bowling
  • Start date Start date
C

Chuck Bowling

Ok, either I'm doing something wrong or the filters in help are worthless...

I'm trying to format a string... Nothing special, just formatting... like
C++...

How do I do a newline? i.e., assigning a string to a textbox...

txt.Text = "My first line...\n My second line...\n My third line...";
 
Chuck Bowling said:
Ok, either I'm doing something wrong or the filters in help are worthless...

I'm trying to format a string... Nothing special, just formatting... like
C++...

How do I do a newline? i.e., assigning a string to a textbox...

txt.Text = "My first line...\n My second line...\n My third line...";


Ok... the newline character works in a string... apparently it just doesn't
get displayed in a textbox control...

Anybody have a clue what's going on? I have the textbox Multiline and
acceptsReturn properties set to True. What am I missing?
 
Chuck Bowling said:
Ok... the newline character works in a string... apparently it just doesn't
get displayed in a textbox control...

Anybody have a clue what's going on? I have the textbox Multiline and
acceptsReturn properties set to True. What am I missing?

You need to use \r\n instead of \n between lines. For instance:

using System;
using System.Windows.Forms;
using System.Drawing;

public class Test : Form
{
Test()
{
Size = new Size(200,200);
TextBox tb = new TextBox();
tb.Size = new Size (150, 150);
tb.Text = "First line\nSecond line\r\nThird line";
tb.Multiline = true;
Controls.Add(tb);
}

static void Main()
{
Application.Run(new Test());
}
}

comes up with

First line Second line
Third line
 
Great. Thanks for your help Jon..


Jon Skeet said:
You need to use \r\n instead of \n between lines. For instance:

using System;
using System.Windows.Forms;
using System.Drawing;

public class Test : Form
{
Test()
{
Size = new Size(200,200);
TextBox tb = new TextBox();
tb.Size = new Size (150, 150);
tb.Text = "First line\nSecond line\r\nThird line";
tb.Multiline = true;
Controls.Add(tb);
}

static void Main()
{
Application.Run(new Test());
}
}

comes up with

First line Second line
Third line
 
There is an
Environment.NewLine Property

Which, in Windows, is equivalent to "\r\n"
It may be different on other platforms, so if you plan on using other
platforms, you may want to use this property instead.

Chris R.
 
Ok. Thanks Chris...

Chris R said:
There is an
Environment.NewLine Property

Which, in Windows, is equivalent to "\r\n"
It may be different on other platforms, so if you plan on using other
platforms, you may want to use this property instead.

Chris R.
 
Back
Top