Question What is the best way to update a RichTextBox?

UncleRonin

Well-known member
Joined
Feb 28, 2006
Messages
230
Location
South Africa
Programming Experience
5-10
Yesterday I was working with a RichTextBox and decided to add some formatting to my logging (I've never formatted text in one before!! O_O) ...not quite what I expected!

Now, normally I work with plain unformatted text and I have an imposed limit on the number of characters I allow to be displayed in the GUI log. So I did my usual thing: After the length exceeds my cap I remove all the oldest lines from the log and set the most recent log info as the .Text property of the RTB... FAIL! All formatting was lost and things were ugly looking again. Sigh.

So eventually I worked around this and ended up using .AppendText to add any text to the RTB and using .Select(0, IndexOfTheFirstLineOfValidText) and then .SelectedText = String.Empty to remove any old text.

Is this the right way to do this or this there a better way out there (which I'm quite positive there must be!)?

Don't get me wrong, this works BUT it really doesn't feel so great...
 
Unless you want to write your own RTF parser, that's about as good as it gets. If you could ensure that no formatting spanned multiple lines then you might also be able to use the Lines property to remove multiple lines at a time, although I've never tried so I'm not sure.
 
Back
Top