views:

600

answers:

1

I am applying formatting to selected tokens in a WPF RichTextBox. To do this I get a TextRange that encompasses the token that I would like to highlight. I will then change the color of the text like this:

// Get start and end pointer for token
TextPointer startPointer = run.ContentStart.GetPositionAtOffset(startOffset);
TextPointer endPointer = run.ContentStart.GetPositionAtOffset(endOffset);

// Get text range for token
TextRange textRange = new TextRange(startPointer, endPointer);

// Highlight token
textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);

This is happening on the TextChanged event of my RichTextBox.

The formatting is applied as expected, but continuing to type text will result in the new text inheriting the formatting that has already been applied to the adjacent word. I would like the formatting of any new text to use the default formatting options defined in the RichTextBox properties. Is this possible?

Alternatively I could highlight all tokens that I don't want be blue with the default formatting options but this feels awkward to me.

+2  A: 

Isn't that the standard behavior? If I highlight text in Word, make it bold, move the cursor next to that text and type, the new text becomes bold too. I'd imagine that MS made the rich text box work similarly to the text area in Word.

If you're selecting a token based on matching some criteria, like for syntax highlighting, could you try applying the style to the text after the cursor is 1 character away from the token? For example:

SomeToken |

instead of

SomeToken|

where | is the cursor. The cursor is separated from the token by a space in the first example, so if your program finds SomeToken it'll get the text range from before the S to before the (space). I'd imagine that the style won't be applied to newly entered text then.

I'll admit this is just a guess, and I might have misunderstood your exact situation from your problem description. Some more code might be helpful, particularly the TextChanged event you mention, or the code that creates the text range.

Benny Jobigan
Thanks for your reply Benny.Yes, this is the standard behavior but I am trying to implement syntax highlighting and would rather it didn't do that. In the TextChanged event I am simply calling my parsing routine which produces tokens that have the start and end positions that I use for creating the TextRange (I have added this code to my original question).The use of whitespace as a delimiter is a possibility but I would rather avoid it as whitespace is not guaranteed.Thanks,Alan
Alan Spark
Well, it doesn't have to be whitespace as a delimiter. What I was suggesting is to **wait** to apply the style until the cursor is at `endOffset + 1`, so there is something between the cursor and the text range. So even after type `SomeToken|` *just* finishing typing "n", the token isn't selected yet. But, when I type the *next* character, `SomeToken{|`, then the token is found and styled when I enter "{" (or any other character). I noticed that syntax highlighting works like that in some other programs. Sorry if I wasn't clear. I hope that's clearer.
Benny Jobigan
Thanks again Benny. Don't worry, you were very clear.What I was trying to avoid was a complete reset of the formatting on each parse but I can still see situations where the behavior of the RichTextBox when it inherits the adjacent formatting that will cause me problems if I don't completely reset formatting. I'm sure there will be a happy medium.Thanks,Alan
Alan Spark
Oh! I just remembered a blog post I read that might be helpful to you! http://www.jeff.wilcox.name/2010/03/syntax-highlighting-text-block/ It's about silverlight, but it should give you some hints (and code to peek at). Jeff Wilcox deeply involved with silverlight, so he knows what's up.
Benny Jobigan
Thanks for the link. Looks interesting.
Alan Spark