

We could transform the geometry before calling the DrawGeometry method. In the case of text highlighting, the geometry that was drawn was exactly the same as the one produced from the FormattedText object. New Point(leftMargin, topMargin - this.VerticalOffset),ĭrawingContext.DrawGeometry(dec.Brush, null, geom) If (dec.DecorationType = EDecorationType.Hilight) The following will give us the same text as originally in the TextBox. Text creation and all that follows depends on the FormattedText class. In this case, the word boundary is before the f rather than the first It is also worth noting that even with well over one hundred words, this Decoration's performance is quite good. Please note that "words" such as will not be found using this Decoration. Public override List Ranges( string Text) Public class MultiRegexWordDecoration:Decoration If one uses this for syntax coloring the MultiRegexWordDecoration is probably the most useful. MultiRegexWordDecoration: selects based on a list of strings enclosed by word boundaries.RegexWordDecoration: Selects based on a string enclosed by word boundaries.MultiRexexDecoration: Selects based on matching any of a list of Regular Expressions.RegexDecoration: Selects based on matching a Regular Expression.MultiStringDecoration: Selects based on String.IndexOf for a list of given strings.StringDecoration: Selects based on String.IndexOf for a given string.MultiExplicitDecoration: Selects based on a list of starting character positions and number of characters.ExplicitDecoration: Selects based on a starting character position and the number of characters.I have included eight Decoration classes. Please note that the list is dependent on the text involved. The classes used to select the text for decoration are all derived from the abstract base class Decoration.

I also needed to take care of scrolling, text coloring, background coloring, underlining, and strikethroughs. I expected that in time I could have quite a number of them. As the original purpose of this control was to highlight text affected by Regular Expressions in various ways, I needed an extensible method for making text selections. There were several issues that needed to be taken care of. Text coloring rules are set through the Decorations property. Hopefully, I will be able to remove this nonstandard behavior soon. In order to set the background color, just wrap it in a border. In order to set the default text color, use the BaseForeground property. This is done in the constructor so all one needs to do is not set them. The background and foreground brushes should both be set to something transparent. This control can be used like a regular textbox with the following caveats. It exceeded my expectations, so here it is. I figured that this couldn’t possibly be slower than the RichTextBox, so I gave it a try. Selecting and editing will be handled by the underling text box functionality.The original text's brush can be set to something transparent so it will not cover up the decorated text.Since both sets of text are visible, I can make sure that they line up exactly.Recreate the same text as was in the textbox, but colorized and otherwise decorated.It took a few moments to progress from Oh that’s odd to Wow, my problem is solved, as I could now do the following. I was surprised to find that both the textbox’s text and my additional text were visible, as I expected only my overridden text to appear. For the heck of it, I overrode the OnRender method, and wrote a little text to the TextBox. I had tried numerous times to somehow get performance that compared to the WinForms RichTextBox, but failed. My problem was the painful slowness of the RichTextBox control. For the n th time, I was considering moving it from WinForms to WPF. I was in the process of upgrading a Regular Expression generating tool of mine. As it is derived from the TextBox control, rather than the RichTextBox, it is quite speedy. This article presents an enhanced text box control that is designed to facilitate text coloring, highlighting, underlining, and striking.
