在 WinForms RichTextBox 中重置 RTF 格式而不丢弃其文本?
我正在尝试“重置” RichTextBox(WinForms,而不是 WPF)中的格式。我以前使用过
richTextBox.Text = richTextBox.Text;
但是,这似乎突然让我失败了。现在,无论我将 richTextBox.Text
设置为什么,它都会保留一些 rtf 格式。
我已经尝试过
richTextBox.Rtf = richTextBox.Text;
但是,它抱怨格式不正确。一定有更好的方法来做到这一点。 (当然,选择整个内容,然后重置背景颜色、前景色和字体是可行的,但这会导致闪烁,因为选择整个内容然后取消选择,而且速度较慢并且需要更多代码。)有什么想法吗?
编辑: 我已经做到了:
string tempTxt = richTextBox.Text;
richTextBox.Clear();
richTextBox.Text = tempTxt;
但是必须有更好的方法,对吗?
编辑2: 需要明确的是,我希望删除所有格式,同时保留文本。看起来第一次编辑中的代码将会发布,除非其他人有更有效/更好的编码方式。
编辑3:
richTextBox.Text = richTextBox.Text.ToString();
似乎不起作用,因为它仍然没有清除所有格式。我不喜欢上面第一个编辑中的方法的原因是,当它清除文本框然后重新输入文本时,它会使文本框“闪烁”。似乎应该简单地有一个 richTextBox.ResetFormatting() 方法,或者某种访问相同功能的方法,因为 Clear() 方法显然(没有双关语)除了简单地清除所有文本之外还执行某种格式重置。
总结一下:
是否有一种方法(如果是的话,它是什么)可以重置 RichTextBox 中文本的格式,而无需像上面的示例那样清除文本(因为这会产生不需要的闪烁)?
I'm trying to "reset" the formatting in my RichTextBox (WinForms, not WPF). I was previously using
richTextBox.Text = richTextBox.Text;
However, that seems to have suddenly failed me. Now no matter what I set richTextBox.Text
to, it retains some of the rtf formatting.
I've tried
richTextBox.Rtf = richTextBox.Text;
However, that complains about an incorrect format. There's gotta be a better way to do this. (Of course, selecting the entire thing, then resetting the back color, fore color, and font works, but that results in a flicker as the entire thing is selected then deselected, plus it's slower and requires a lot more code.) Anyone have any idea?
Edit:
I've gotten this to work:
string tempTxt = richTextBox.Text;
richTextBox.Clear();
richTextBox.Text = tempTxt;
But there has to be a better way, right?
Edit 2:
To be clear, I wish to remove all formatting while retaining the text. It looks like the code in the first edit will ship, unless anyone else has a more efficient/better coding way.
Edit 3:
richTextBox.Text = richTextBox.Text.ToString();
doesn't seem to work because it still doesn't clear all the formatting. The reason I don't like the method in the first Edit above is it makes the text box "flash" when it clears it then re-inputs the text. It seems like there should simply be a richTextBox.ResetFormatting() method, or some way to access the same functionality, as the Clear() method clearly (no pun intended) does some sort of formatting reset in addition to simply clearing all the text.
To summarize:
Is there a way (and if so, what is it) to reset the formatting of the text in a RichTextBox without clearing the text as in the example above (because that produces undesirable flashing)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
遗憾的是,我已尽最大努力将其精简为仅所需的代码。它仍然很大,但它会起作用。 .Net 中的 RichTextBox api 非常有限,要做任何事情你几乎都必须进入 Win32 库。我围绕这个东西构建了一个完整的库,这样我就可以切换粗体并确定粗体是否确实在整个选择中设置。
用法:
大量代码:
您还想了解更多吗?
我通过一个小型实用程序类来使用其中的大部分内容,该实用程序类将其包装为所有样式和字体变化。这样您可以更改字体大小而不更改字体名称等。
Saddly I've done my VERY best effort to slim this down to only the required code. It's still big, but it will work. The RichTextBox api in .Net is very limited, to do anything you almost have to thunk into the Win32 library. I've built a entire library around this thing just so I can toggle bold and determine if bold is actually set across the selection.
Usage:
Tons of code:
More you ask?
I use most of this via a small utility class that wraps this for all the styles and font changes. This way you can change font-size and not change font name, etc.
怎么样
What about
我已经使用过
EDIT::
请务必插入一条评论,说明您为什么要做您正在做的事情。对于不知情的人来说,这看起来很荒谬。
I have used
EDIT::
be sure to insert a comment as to why you're doing what you're doing. To the unaware, it looks ridiculous.
只需使用:
...应该可以解决问题。对我有用。
Just using:
... Should do the trick. Works for me.
我发现的另一种方法(也是我已切换使用的方法,因为它不闪烁)是在应用任何格式之前获取初始 rtf 字符串:
然后,当我想重置格式时,我可以do:
但是,这并不是很完美,因为它要求文本保持不变等等。嗯,至少比问题中详细介绍的方法好一点。
Another way I've found (and the one that I have switched to using, as it doesn't flash) is to grab the initial rtf string before any formatting is applied:
Then, when I want to reset the formatting, I can just do:
However, this isn't really perfect, because it requires that the text stay the same and such. Well, at least it's a little better than the method detailed in the question.
您应该能够实现 SuspendLayout() 和 ResumeLayout() 方法。
SuspendLayout() 和 ResumeLayout() 将在您操作数据时停止控件的绘制。如果操作不需要很长时间,您将能够清除文本并将未格式化的文本分配回来,而不会在屏幕上闪烁。
如果花费的时间太长,控件将显示为黑色矩形,直到调用 ResumeLayout() 为止。
You should be able to implement the SuspendLayout() and ResumeLayout() methods.
SuspendLayout() and ResumeLayout() will stop the control from drawing while you manipulate the data. If the operation doesn't take to long, you will be able to clear the text and assign the unformatted text back without it appearing to flash on the screen.
If it does take too long, the control will appear as a black rectangle until ResumeLayout() is called.
一段时间以来,我一直在自己的程序中使用这段代码。它直接设置 RichTextBox 的 RTF,因此应该比通常的方式设置样式要快得多。它接受一个字符串(主文本),并且还可以选择接受颜色数组、字体大小数组(int)和字体粗细数组(bool),每个数组代表每个字符的每种颜色、大小或字体粗细字符串。
或者,您可以简单地保留标题给出的默认尺寸、重量、斜体数字。
For a while, I've been using this code in my own program. It sets the RTF of the RichTextBox directly, so should be much faster than setting styles the usual way. It takes in a string (the main text), and optionally also takes a Color array, font size array (int), and font-weight array (bool), each which represents every colour, size or font-weight for every character in the string.
Alternatively, you can simply keep the default size, weight, italic numbers given by the header.
我看到有很多答案,但我认为有更简单和容易的方法作为清除所有格式的扩展:
在我的情况下,我需要清除格式并留下一个空的
RichTextBox
,因此我做了这个函数:那么简单的解决方案是:
或者简单地删除clear函数中的
this.richTextBox1.Clear();
行。 但我不能保证,因为我只在简单的格式上进行了测试。因此,应该添加任何其他行来删除不同的格式。)(这可能也有效, 不清除请记住在数据格式化后存储先前的位置/选择和刷新状态。
I see that there are many answers, but I think that there is more simple and easy approach as the extension to clear all the formatting:
In my case I needed clear the formatting and leave an empty
RichTextBox
, therefore I made this function:Then the easy solution is:
Or simply remove the
this.richTextBox1.Clear();
line in the clear function. (This will probably also work but I give no guarantee since I tested this only on simple formattings. Therefore it can be, that any other line should be added to remove a different formatting.)When the text should be not cleared remember to store previous position/selection and refresh state after deformatting of data.
希望它有效
hope it works
工作正常..
works fine ..