Search code examples
mysqlvb.nettextbox

Newline data not showing up correctly in vb.net multiline TextBox


I'm populating a multi-line TextBox from MySQL, and the data is basically paragraphs with multiple lines with line breaks in between. My data looks fine in the database; when I copy and paste the data into a text editor (I use NotePad++) directly from workbench, the data looks fine.

enter image description here

There's a curious space between Test2 and Test3, but I can live with that.

When I try and MsgBox it, it looks fine too:

enter image description here

But when it comes to adding it to my TextBox, my problem comes:

enter image description here

I've looked at several resources, including here, but I couldn't find a solution that worked, nor a reasonable explanation why this is happening. Does anyone at least have an explanation so I can figure out a workaround?


Solution

  • It appears, from the text pasted into Notepad++, that the original string copied from MySql Workbench, contains multiple line feed chars and, probably, a space which separates the text parts.

    This becomes more evident, since a MessageBox shows the text separated in multiple lines.
    The .Net MessageBox wraps the User32 MessageBox function, which allows \n (VbLf - CharW(10)), \r (VbCr - CharW(13)) or both (VBCrLf) as line separator(s).
    The string parameter of the message part, lpText, will parse and adapt any of these:

    The message to be displayed. If the string consists of more than one line, you can separate the lines using a carriage return and/or linefeed character between each line.

    A standard TextBox instead requires that a LineFeed+Carriage Return (\r\n) are used to separate lines of text: when the text is pasted in a TextBox control, since only \n is used, the text is not separated in different lines.

    The RichTextBox control is more international (MacOs also ditched \r for the *nix \n): it uses \n to separate lines of text.
    If we provide \r\n (Windows style) as line separator, the control converts \r\n to just \n.
    Thus, pasting this text in a RichTextBox, the original line feeds are parsed as intended.