New line is not working in MessageBox in C#/WPF

Try this:

    String outputMessage = string.Format("Line 1{0}Line 2{0}Line 3", Environment.NewLine);
    MessageBox.Show(outputMessage);

A further example with another variable:

    String anotherValue = "Line 4";
    String outputMessage = string.Format("Line 1{0}Line 2{0}Line 3{0}{1}", Environment.NewLine, anotherValue);
    MessageBox.Show(outputMessage);

Put a place holder where you want to put new line and in the code where you use that resource string, just replace it with new line: string resource: "This is first line.{0}This is second line.{0}This is third line." You will use this resource string like this: MessageBox.Show(string.Format(MyStringResourceClass.MyStringPropertyName, Environment.NewLine));

OR

Unconventional Method But i just now got it working by coping newline from word directly (or anyother place) & pasting it inside the resource string file.

It was simple..
OR

\r\n characters will be converted to new line when you display it by using message box or assign it to text box or whenever you use it in interface.

In C# (like most C derived languages), escape characters are used to denote special characters such as return and tab, and + is used in place of & for string concatenation.

To make your code work under C# you’ve got two options... the first is to simply replace the NewLine with the return escape character \n ala:

MessageBox.Show("this is first line" + "\n" + "this is second line");

The other method, and more correct is to replace it instead with Environment.NewLine which theoretically could change depending on the system you are using (however unlikely).

MessageBox.Show("this is first line" + Environment.NewLine + "this is second line");

In the resource editor seperate your string content by using shift+enter. Or else, edit your ResX file in xml editor and using enter key create a new line for your resource string.

Refer this link for detail info: Carriage Return/Line in ResX file.

Tags:

C#

Wpf

Messagebox