将文本分配给字符串变量后,如何在 Winforms 中保留 TextBox 换行符? [英] How do I retain TextBox line breaks in Winforms after assigning text to a string variable?

查看:38
本文介绍了将文本分配给字符串变量后,如何在 Winforms 中保留 TextBox 换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多行文本框的 WinForms 应用程序.这可以很好地显示和保留(从数据库加载后)换行符.

I have a WinForms app with a multi-line textbox. This displays and retains (after loading from the DB) line break characters fine.

但是,如果我将 TextBox.Text 值分配给一个字符串变量,然后将该变量重新分配回 TextBox.Text 属性,则换行符将丢失并替换为方形字符(不能在此处跳过它们)因为它们只是作为换行符粘贴!)

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!)

我试过了:

Replace("\n",vbcrlf)

但无济于事.

谁能提出解决方案?

更新**

我还没有解决这个问题,但已经通过避免变量赋值来解决这个问题.我现在传递对 RichTextBox 的引用并直接操作那里的文本.请注意,这似乎是 RichTextBox 特有的,因为我没有看到普通 TextBox 的问题.

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.

推荐答案

代替 Replace("\n"' vbcrlf),您需要使用 Replace(vblf, vbcrlf) 或 Replace(vbcr, vbcrlf).

Instead of Replace("\n"' vbcrlf), you need, to use either Replace(vblf, vbcrlf), or Replace(vbcr, vbcrlf).

这篇关于将文本分配给字符串变量后,如何在 Winforms 中保留 TextBox 换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆