1

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?

Wakka02
  • 1,491
  • 4
  • 28
  • 44
  • Which one [of the three](https://stackoverflow.com/q/27223228/11683) is your data using? – GSerg Apr 18 '19 at 12:36
  • Does it work if you use a RichTextBox instead of a TextBox? – Jimi Apr 18 '19 at 12:45
  • @Jimi, my good sir, it does indeed. Thank you so much! – Wakka02 Apr 18 '19 at 13:16
  • 1
    That means that you're carrying over `\n` (`VbLf`) instead of `\r\n` (`VBCrLf`). RichTextBox uses `\n` as the new line char, while the TextBox control needs both (Windows style). – Jimi Apr 18 '19 at 13:17
  • In notepad++ Click View>Show Symbols>Show All Chars What is being show for your line spaces? – Mr. Tripodi Apr 18 '19 at 13:23
  • @Jimi perhaps you might want to include that as an answer so I can mark it as the answer – Wakka02 Apr 20 '19 at 01:54

1 Answers1

0

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.

Jimi
  • 29,621
  • 8
  • 43
  • 61