Search code examples
c#messagebox

Read formatted (downloaded) text into messagebox


By downloading a string from the web, is it possible to make the messagebox read values like \r\n or Environment.Newline, that are written inside the text downloaded ?

The online text message contains: First line \r\n Second line (into a textfile)... i would like to out put this inside the messagebox as it is formatted in the file.

var message = await RemoteHandler.GetWebContent(RemoteHandler.RemoteMessageUrl);
MessageBox.Show(message, "Title", MessageBoxButtons.OK, MessageBoxIcon.Information);

Current output:

First line \r\n Second line

Wanted output:

First line
Second Line


Solution

  • After some different test cases, I have determined that your \r\n is most likely stored as \\r\\n in your string which would cause the \r\n to print as a string literal instead of an Environment.NewLine. You should use one of the the following code to correct this string:

    message = message.Replace("\\r\\n", Environment.NewLine);
    message = message.Replace(@"\r\n", Environment.NewLine);
    

    By using the double backslash, the carriage return and new line characters are being escaped and thus being printed literally. This is either a data issue or an issue with how the string is being loaded from the remote resource. However, this should get your new lines printing correctly within the message box.