Frage

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

War es hilfreich?

Lösung

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");

Andere Tipps

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.

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);

Try this

removing the MessageBoxButtons.OK and MessageBoxImage. Information.

 MessageBox.Show(String.Format(Properties.Resources.About, 
Constants.VERSION), 
Properties.Resources.About_Title);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top