如何在richtextbox中添加空间 [英] how add space in richtextbox

查看:70
本文介绍了如何在richtextbox中添加空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Richtextbox.
在这个Richtextbox中,我添加了一些图片和文字
当我使用
richTextBox1.Text + =";
为了在richtextbox中添加空间,图片被清除.
而且我不想使用appendtext

I have a richtextbox.
in this richtextbox i add some picture and text
when i use
richTextBox1.Text += " ";
For add space in richtextbox the picture is cleared.
and i don''t want use of appendtext

推荐答案

添加+ " "无效.首先,字符串是不可变的,因此+不会更改字符串,它会创建一个全新的字符串,并复制所有旧内容以及该新字符串中的新内容.其次,当文本很长时,读取然后写回属性Text变得非常慢.

请参阅我过去的回答中的更多详细说明: Mutable和Immutable之间的区别C#中的字符串 [ ^ ].

您的图片消失了,因为您分配了文本并且格式不正确.此属性将控件的内容视为纯文本.您将需要操作属性Rtf,但是由于我已经解释的原因,它会很慢.

正确的答案是从控件本身询问内容的长度,在最后选择文本且选择结尾等于零的文本.然后,您需要分配选定的文本"属性.您可能还想记住旧的光标位置,并在追加后将其恢复.

看,您无法使用UI库标记问题,并且未指定RichTextBox的确切类型.您为我造成了一个问题,因为以伪代码的形式编写答案(请参见上一段)很不方便.您也没有收到任何代码.因此,现在尝试自己编写代码.不想仅仅因为您没有正确地问这个问题而写两个版本.如果您有任何问题,我会回答.

祝你好运,
—SA
Adding + " " is doubly ineffective. First, strings are immutable, so + does not change a string, it creates a brand new string and copies all old content plus a new one in this new string. Secondly, reading and then writing back the property Text becomes dead slow when the text is long.

See more detailed explanation in my past answer: Difference Between Mutable , Immutable string in C#[^].

Your picture disappears because you assign text and loose formatting. This properties treats the content of the control as the plain text. You would need to operate the property Rtf instead, but it would be slow by the reason I already explained.

Correct answer is asking the length of the content from the control itself, selecting the text at the very end with selection ends equal to zero. Then you need to assign the "selected text" property. You may also want to remember old cursor position and restore it after appending.

Look, you failed to tag your question with your UI library and did not specify exact type of RichTextBox. You created a problem for me as writing the answer in the form of pseudo-code (see previous paragraph) is inconvenient. You also did not receive any code. Therefore, now try to write the code yourself. In don''t want to write two version just because you did not ask the question properly. If you have any questions, I''ll answer them.

Good luck,
—SA


这篇关于如何在richtextbox中添加空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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