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)?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…