MessageBox problems
I am developing an application that contains an error database. This contains a list of error numbers, names and text associated with them so knowing an error number can show a MessageBox with the name and text in it.
The problem is that it doesn't recognise '\n' as a new line, instead it writes '\n'. BUT this only happens when the text is called from the database.
If this doesn't make sense here is some code: (C#)
string text = this.thisError.text;
//This will show "Example \n error"
MessageBox.Show(text, caption, button, icon);
text = "Example \n error";
//This will show "Example
//error";
MessageBox.Show(text, caption, button, icon);
Now "text" is a string for both examples the only difference is one is being called from a database, sent to a struct called thisError where there is a string called text. They contain the same data WHY does it display the '\n' when I want the MsgBox to show a new line.
Help would be appreciate as I am crying (ish) as I write this.
cheers
When you put a string literal in your C# source (text = "Example \n error") the compiler substitutes the \n escape sequence with a real 0x0A newline at compile time. You can see this by examining the memory buffer for that string in the debugger at run-time.
I believe MessageBox.Show just passes the string straight down to the OS for display, and the OS's DrawText() will interpret the 0x0A charcater as a line break. I don't believe MessageBox has any kind of escape-code interpreting or line-breaking capabilities of its own.
If you enter a string like "Example \n error" into a database field via some database app, its probably gonna just store it as-is, with "\n" as two characters. When your app sucks this string out of the database later, the "\n" is still two characters. You need to find a way to get 0x0A characters into the strings at the database end, or peform some kind of find/replace routine in your C# code as suggested by DarkByte.
Hope this helps,
Iain Heath,
WinForms Team.
I believe you wanted to know why i used "\\n" in the Replace function.
As Iain explained, the compiler interprets the string and replaces "\n" at compile time. If i had used text.Replace("\n"...) the compiled code would think "look for a char 0x0a (NewLine) and replace it with something else".... but what i want is for it to search the 2 characters "\" and "n". In a string constant in the source code, the "\" char is used as escape char and allows for "\n" to be newline, or "\t" to be tab, etc etc. If i really want to look for "\", i need to escape it. So "\\" is interpreted and stored as "\".
Hope its more clear now.