How do I retain TextBox line breaks in Winforms after assigning text to a string variable?

自作多情 提交于 2019-12-24 10:13:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!