TX Spell .NET for WPF

RichTextBox

Topic

The WPF RichTextBox can be spell checked with TX Spell .NET alone: the package TXTextControl.TXSpell.WPF.SDK is all it takes, and TX Text Control isn't needed. What differs from TX Text Control is who does the work. TX Text Control underlines, corrects and offers its menu by itself; with a RichTextBox, TX Spell .NET finds the mistakes and your code shows and fixes them in the FlowDocument. The tutorials in RichTextBox build each part step by step; here is what the integration consists of, and what to expect from it.

What TX Spell .NET Does and What Your Code Does

TaskWith a RichTextBox
Initialize the spell checkerYour code calls Create once; no TextControl does it for you
Find misspelled wordsCheck with the text of the document fills IncorrectWords
Mark the wordsYour code colors them; there is no wavy underline
Offer the dialogSpellCheckDialog.Show(this) walks the user through the words of the last check
Apply a correctionYour code changes the document in the IncorrectWordChanging event
Offer a context menuGetMenuItems creates the items; your code adds them to a ContextMenu

The spell checker, declared as a resource of the window, works on a string. The text of a RichTextBox is new TextRange(document.ContentStart, document.ContentEnd).Text, and that string is what you pass to Check.

Positions in a FlowDocument

The Start of an IncorrectWord is the zero-based index of its first character in the checked string, not a position in the FlowDocument. To color or replace the word, your code walks the document with TextPointer objects to the same character, counting each paragraph end and line break as Environment.NewLine, the way TextRange.Text writes them. The helpers GetTextPointerAtOffset and GetRange in Check Spelling as You Type do this; the range they return is colored with ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red) and replaced by setting its Text.

A TextBox doesn't need this: its character positions are those of its Text, and textBox1.Select(incorrectWord.Start, incorrectWord.Length) selects the word.

Mark Misspelled Words as the User Types

The TextChanged event of the RichTextBox checks the text and colors each incorrect word red:

Four RichTextBoxes, in three of which the misspelled word wordd is colored red.

Because the whole text is checked on every change, a word turns red while it is still being typed, and the check takes longer as the text grows. TX Text Control, by comparison, underlines only completed words. Check Spelling as You Type builds this application.

A TextBox can't color single words, but a validation rule can report mistakes while the user types:

A TextBox containing Testt, with the red message Misspelled next to it.

Validate a TextBox as the User Types shows the rule.

Correct in the Dialog

The spell check dialog works with any text: call Check first, then SpellCheckDialog.Show(this) with the window as the owner. When the user changes or deletes a word, IncorrectWordChanging fires, and your code makes the same change in the document. Open the Spell Check Dialog builds this for a RichTextBox, and Check a TextBox in the Dialog explains the arguments of the event.

Correct in a Context Menu

A RichTextBox has no spell check menu of its own. The overload GetMenuItems(IncorrectWord, string, int) takes the word under the mouse, the text of the document and the maximum number of suggestions, and returns MenuItem objects with the suggestions and the spell checking commands:

A context menu over a misspelled word in a WPF RichTextBox, with suggestions followed by the commands Ignore, Ignore All, Add To Dictionary, Spell Check Dialog and Options.

A correction from the menu raises IncorrectWordChanging as well, so the handler of the dialog serves the menu too. A Menu for a RichTextBox builds the menu.

Several Controls

One spell checker serves any number of controls, which saves memory and time compared to one instance per control. IncorrectWords always holds the result of the last Check, so check each control right before you mark its words. To walk through several controls in one dialog, pass the next text to the dialog in its closing event:

Four RichTextBoxes and the Spelling dialog, which shows the first sentence with the misspelled word sampke in bold.

Check Several TextBoxes in One Dialog shows the technique.

Samples

The setup installs samples for each technique, in C# and Visual Basic, below TX Spell 11.0.NET for WPF\Samples\WPF in your Documents folder. Spell Checking Examples lists them.

Next Steps