问题
I have a WinForms app with a multi-line textbox. This displays and retains (after loading from the DB) line break characters fine.
However if I assign the TextBox.Text value to a string variable and then re-assign the variable back to the TextBox.Text property, the line break characters are lost and replaced with a square character (can't past them here as they just paste as a line break!)
I've tried:
Replace("\n",vbcrlf)
but to no avail.
Can anyone suggest a solution?
UPDATE**
I haven't managed to fix this but have worked around it by avoiding the variable assignment. I now pass a reference to the RichTextBox and directly manipulate the text there. Note that this seems specific to the RichTextBox as I don't see the issue with a normal TextBox.
回答1:
Instead of Replace("\n"' vbcrlf), you need, to use either Replace(vblf, vbcrlf), or Replace(vbcr, vbcrlf).
回答2:
Just an idea: How about fixing “\r\n” instead of “\n”?
回答3:
Are you transforming the string anyway? Since something like this:
Dim s As String = TextBox1.Text
TextBox1.Text = s
works well.
回答4:
textBox.AppendText("your new text" & Environment.NewLine)
来源:https://stackoverflow.com/questions/7637092/how-do-i-retain-textbox-line-breaks-in-winforms-after-assigning-text-to-a-string