Creating a styled string for a RichTextBox

66 Views Asked by At

I'm trying to color parts of the text based on labels. I wrote a function that cycles through and does it just fine yet it requires selection which jumps the box around quite frankly it's ugly.

Is there a way to create styled text, concatenate it, and inject it into the RichTextBox?

1

There are 1 best solutions below

1
On BEST ANSWER

RichTextBox can be a little weird so you'll need to test this to see, but in general the approach to stopping this is:

richText.SuspendLayout();
// Update properties
richText.ResumeLayout(true);

This instructs ensures that the control won't be redrawn until you've finished changing properties.

If you really want to ensure the view doesn't jump, you'll need to also store the selection beforehand, then reset it just before you resume the layout.