Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

wpf

messagebox

Short question: I have a string in my resources: "This is my test string {0}\n\nTest"

I'm trying to display this string in my Messagebox:

MessageBox.Show(String.Format(Properties.Resources.About, 
    Constants.VERSION), 
    Properties.Resources.About_Title, MessageBoxButton.OK, 
    MessageBoxImage.Information);

However I don't get new lines. The \n still show up as characters, not as new lines.

I also tried to use a workaround like mystring.Replace("\n", Environment.NewLine) but this also doesn't change anything.

What am I doing wrong?

Edit: Funny thing to mention: Replace("\n", "somethingelse") doesn't change anything.

Edit2: Shift+Enter in my Resource-File instead of \n seems to work... Strange behaviour anyway

like image 642
Frame91 Avatar asked Mar 14 '14 10:03

Frame91


People also ask

How to add new line in message box?

Summary. If you want to force a new line in a message box, you can include one of the following: The Visual Basic for Applications constant for a carriage return and line feed, vbCrLf. The character codes for a carriage return and line feed, Chr(13) & Chr(10).

How to start a new line in a message box c#?

\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.


3 Answers

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");
like image 160
Arjun Chaudhary Avatar answered Oct 21 '22 22:10

Arjun Chaudhary


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.

like image 39
petchirajan Avatar answered Oct 21 '22 23:10

petchirajan


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);
like image 41
Chris Avatar answered Oct 21 '22 21:10

Chris