New line is not working in MessageBox in C#/WPF
Asked Answered
A

5

24

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

Astromancy answered 14/3, 2014 at 10:29 Comment(6)
Unfortunately not. It shows \r\n in plaintextAstromancy
This is interesting ..when I use OP's string by assigning to a variable it works..But when I place it in the Resources it doesn't work.Orella
Is your resource file is in ResX format? did you use VS resx editor to add resource string?Csc
Environment.NewLine will give "\r\n" when run on Windows. If you are generating strings for Unix based environments, you don't want the "\r"Buseck
when the string is read from string table '\r\n' becoming '\\r\\n' thats why shift+enter working not \r\nBuseck
In regards to the Replace("\n", "somethingelse") doesn't change anything comment, that is because you should be looking for \\n instead of just \n. What you think is a new line reference is more than likely being escaped by the time you try to replace it. If you have a situation where you pull text from a DB and spit it out to a MessageBox then try to use MessageBox.Show(_settings.ContactInfo.Replace("\\n","\n"));.Suffer
B
33

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");
Buseck answered 14/3, 2014 at 10:33 Comment(8)
That doesn't answer the question at all? OP already using \n but asking why it is output as-is rather than being shown a line-break.Contradiction
In your case MessageBox.Show("This is my test string {0}"+"\n"+"\n"+"Test");Buseck
but how can I do this within my resources? I'd like to save my whole string in resourcesAstromancy
I think between "Environment.NewLine" should not be quotes.Astor
the problem is that I can't split my string. I just want to load the string from my resources with linebreaks.Astromancy
\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.Buseck
Environment.Newline should be outside the quotes, now you're just concatenating "Environment.NewLine" to the rest.Baecher
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));Buseck
C
7

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.

Csc answered 14/3, 2014 at 11:5 Comment(1)
indeed shift+enter is the easiest way !Usurpation
Q
6

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);
Quadratic answered 14/3, 2014 at 10:45 Comment(1)
add MessageBoxButtons.YesNoCancel then it will not break lines...Wurtz
M
1

Easy solution : in the resource designer : use ctrl+Enter to insert a new line in the string.

Masterstroke answered 20/11, 2023 at 10:43 Comment(0)
M
0

Try this

removing the MessageBoxButtons.OK and MessageBoxImage. Information.

 MessageBox.Show(String.Format(Properties.Resources.About, 
Constants.VERSION), 
Properties.Resources.About_Title);
Memorialist answered 10/2, 2019 at 14:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.