New lines (\n) in Text boxes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In the C++ version of MessageBox() I could embed \n to get a new line. In the C# version of this the \n shows up as \n.

How can I tell the message box (or a textBox on a custom form) to recognize the \n as a newline character and display whatever comes after the \n on a new line

This is real basic, but I'm not able to get it to work
- Bruce
 
Use Environment.Newline, like:

MessageBox.Show("Line 1" + Environment.Newline + "Line 2");

Hope this helps

Javier Campos


In the C++ version of MessageBox() I could embed \n to get a new line. In
the C# version of this the \n shows up as \n.
How can I tell the message box (or a textBox on a custom form) to
recognize the \n as a newline character and display whatever comes after the
\n on a new line.
 
* "=?Utf-8?B?YnJja2NjQG5vdmVsbC5jb20=?= said:
In the C++ version of MessageBox() I could embed \n to get a new line.
In the C# version of this the \n shows up as \n.

How can I tell the message box (or a textBox on a custom form) to
recognize the \n as a newline character and display whatever comes after
the \n on a new line.

Inside a string literal, you can still use "\r\n". You can use
'Environment.NewLine' instead of the combination:

\\\
MessageBox.Show(
"Hello" + Environment.NewLine +
"World\r\n"
"!"
);
///
 
This works fine for me
MessageBox.Show("Test Line 1\nTest Line 2")

Are you doing this instead
MessageBox.Show(@"Test Line 1\nTest Line 2")

The second method will give you the behavior you describe.
 
* "=?Utf-8?B?YnJja2NjQG5vdmVsbC5jb20=?= said:
I have more information. These messages "hello\nthere" are coming
from a resource (resx) file. When the message is read in from the
resource file it adds an additional backslash, thus yielding
"hello\\nthere", so I guess the more appropriate question is, how can I
read this resource and not get the extra backslash from the .resx file?

Right -- that won't work as expected because the "\n" is converted to
the according character at runtime when using "\n" iside a string
literal.
 
So how do I remove or change the @ string type, either with ResourceManager or programmatically
- Bruc

----- Herfried K. Wagner [MVP] wrote: ----

* "=?Utf-8?B?YnJja2NjQG5vdmVsbC5jb20=?= said:
I have more information. These messages "hello\nthere" are comin
from a resource (resx) file. When the message is read in from th
resource file it adds an additional backslash, thus yieldin
"hello\\nthere", so I guess the more appropriate question is, how can
read this resource and not get the extra backslash from the .resx file

Right -- that won't work as expected because the "\n" is converted t
the according character at runtime when using "\n" iside a strin
literal
 
Back
Top