20

Basically i display the some text in the MessageBox with Ok and Cancel button in WindowPhone 7.1.

I need the requirement like the below.

Some text will be here....

Property:value...

Actually we can simply the text in the MessageBox, but how can i add the linebreak between the text in the MessageBox.. Is there any way to do this in Windows Phone?

David Bekham
  • 2,175
  • 3
  • 27
  • 56

7 Answers7

50

You can use Environment.Newline for line breaks

string msg = "Text here" + Environment.NewLine + "some other text";
Charleh
  • 13,749
  • 3
  • 37
  • 57
  • 1
    Actually, from what I've seen it only expects `\n`, not a full newline. A little-known feature of message boxes is that you can copy its text out (on PC anyway) by pressing `Ctrl+C`, and when you do that you'll see that all `\n` characters will be replaced by a full `Environment.NewLine`. However, if you used the full newline _already_, `\r\n`, then that will result in lines separated by `\r\r\n`, which will show as double lines in some applications. – Nyerguds Feb 02 '18 at 11:15
8
MessageBox.Show("Line 1" + Environment.NewLine + "Line 2");
KF2
  • 9,887
  • 8
  • 44
  • 77
3

You can try

\n or <br /> for line Breaks. I am not sure if this will work:

Example:

string msg = "Some text will be here\nProperty:value";

MessageBox.Show(msg);
Raab
  • 34,778
  • 4
  • 50
  • 65
SHAKIR SHABBIR
  • 1,295
  • 1
  • 14
  • 25
3
 MessageBox.Show("aa" + Environment.NewLine + Environment.NewLine + "bb");
hriziya
  • 1,116
  • 1
  • 23
  • 41
2

This is a old post, but... If your text comes from resource file, none of the suggested solution works. In VS resource editor, you have to use Shift+Enter to enter new line. All others will just show as raw text like "\n" or, "\r\n" or "<br />".

Nyerguds
  • 5,360
  • 1
  • 31
  • 63
newman
  • 6,841
  • 21
  • 79
  • 126
1

There are 2 options \n and Environment.NewLine

Option 1: \n

I don't know if this work for sure on Windows phone but I think it would

\n - New line. Place as much as sentences as you want

MessageBox.Show("Some Text Here In The Line NO.1\nSome Text Here In Line NO.2");

Will show:

Some Text Here In The Line NO.1
Some Text Here In Line NO.2

OR

MessageBox.Show("Some Text Here In The Line NO.1 +"\n" + "Some Text Here In Line NO.2");

Will show the same as the first one:

Some Text Here In The Line NO.1
Some Text Here In Line NO.2

Option 2: Environment.NewLine

Environment.NewLine - New line. Place as much as sentences as you want

MessageBox.Show("Some Text Here In The Line NO.1" + Environment.NewLine + "Some Text Here In Line NO.2");

Will show the same as the first one:

Some Text Here In The Line NO.1
Some Text Here In Line NO.2

From msdn.microsoft

The functionality provided by NewLine (Environment.NewLine) is often what is meant by the terms newline, line feed, line break, carriage return, CRLF, and end of the line.

I prefer \n because is shorter and faster but whatever you like.

Raz Luvaton
  • 3,166
  • 4
  • 21
  • 36
  • Just `\n` is more correct too, internally; the message box seems to replace all `\n` characters in the given string by the platform's `Environment.NewLine` when copying the text box info (ctrl+c), so with full `\r\n` line breaks in the initial text you'll end up with messed up `\r\r\n` in the copied text. – Nyerguds Feb 02 '18 at 11:19
1

If you have a very very large message to display, use:

MessageBox.Show(String.Join(Environment.NewLine,
                            "Line 1",
                            "Line 2",
                            "Line 3"));
kwyntes
  • 1,045
  • 10
  • 27