Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - MessageBox - Message localization in resources and lines breaks

I would like to show MessageBox (WinForms) with string from Resources with lines breaks.

example without Resources (WORKS):

string someMsg = "Message. Details:\n" + someDetails;

MessageBox.Show(someMsg);

Result:

Message. Details:

here are some details

When I move string "Message. Details:\n" into Resources:

string someMsg = GlobalStrings.MsgBoxJustTest + someDetails;
MessageBox.Show(someMsg);

Result:

Message. Details:\nhere are some details

When I moved string with "\n" to resources then MessageBox.Show() stopped to interpret it as newline.

Edit: I'm thinking about: someMsg.Replace(@'\n',Environment.NewLine); but it's still quite annoying for so simple thing.

like image 493
binball Avatar asked May 20 '11 11:05

binball


2 Answers

if you add that to resources it doesn't take \n as escape charecter Just open your resource file in notepad to see this and cahnge in XML file(resx)

or

Type your data in notepad with new line. Copy that and paste in your resource editor

edit:

or

Type/Paste your data into the resource editor UI, select the \n and replace it with an actual linebreak, with Shift-Enter.

like image 192
Umesh CHILAKA Avatar answered Oct 31 '22 23:10

Umesh CHILAKA


You could do something like this (as long as your not .net 2.0):

public static class StringExt
{
  public static String FixNewLines(this String str)
  {
    return str.Replace(@'\n',Environment.NewLine);
  }
}

And then:

string someMsg = GlobalStrings.MsgBoxJustTest + someDetails;
MessageBox.Show(someMsg.FixNewLines());

However, this will affect ALL strings in your application (namespace scope)

It's a dirty fix, but it's a quick fix.

Personally, I would just fix my logic all the way through, rather than do something like the above.

like image 26
Theofanis Pantelides Avatar answered Oct 31 '22 22:10

Theofanis Pantelides