# RichTextBox

> Spell check a WPF RichTextBox with TX Spell .NET: map IncorrectWord positions to a FlowDocument, color words, correct in the dialog or a GetMenuItems menu.

- **Product:** TX Spell .NET for WPF
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Spell .NET for WPF > Integration Guide > RichTextBox
- **1370 words**
- **Web URL:** https://docs.textcontrol.com/tx-spell-dotnet-wpf/integration/richtextbox/
- **LLMs URL:** https://docs.textcontrol.com/tx-spell-dotnet-wpf/integration/richtextbox/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-spell-dotnet-wpf/integration/richtextbox/llms-full.txt

---

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](https://docs.textcontrol.com/tx-spell-dotnet-wpf/getting-started/richtextbox/llms-full.txt) 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

| Task | With a RichTextBox |
|---|---|
| Initialize the spell checker | Your code calls [Create](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/create-method/llms-full.txt) once; no `TextControl` does it for you |
| Find misspelled words | [Check](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/check-method/llms-full.txt) with the text of the document fills [IncorrectWords](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/incorrectwords-property/llms-full.txt) |
| Mark the words | Your code colors them; there is no wavy underline |
| Offer the dialog | [SpellCheckDialog](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/spellcheckdialog-property/llms-full.txt)`.Show(this)` walks the user through the words of the last check |
| Apply a correction | Your code changes the document in the [IncorrectWordChanging](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/incorrectwordchanging-event/llms-full.txt) event |
| Offer a context menu | [GetMenuItems](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/getmenuitems-method/llms-full.txt) 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](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/spelledword/start-property/llms-full.txt) of an [IncorrectWord](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-proofing/incorrectword/llms-full.txt) 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](https://docs.textcontrol.com/tx-spell-dotnet-wpf/getting-started/richtextbox/as-you-type/#write-the-code) 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.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-wpf/richtextbox-multi-controls.9a8f2c08.webp)

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](https://docs.textcontrol.com/tx-spell-dotnet-wpf/getting-started/richtextbox/as-you-type/llms-full.txt) 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.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-wpf/textbox-validation-binding.9a3271d0.webp)

[Validate a TextBox as the User Types](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/as-you-type/#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](https://docs.textcontrol.com/tx-spell-dotnet-wpf/getting-started/richtextbox/dialog/llms-full.txt) builds this for a `RichTextBox`, and [Check a TextBox in the Dialog](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/on-demand/#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](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/menuitem/llms-full.txt) 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.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-wpf/richtextbox-context-menu.e10b1dc8.webp)

A correction from the menu raises `IncorrectWordChanging` as well, so the handler of the dialog serves the menu too. [A Menu for a RichTextBox](https://docs.textcontrol.com/tx-spell-dotnet-wpf/user-interface/context-menus/#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.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-wpf/richtextbox-multi-dialog.b8007266.webp)

[Check Several TextBoxes in One Dialog](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/multiple-controls/#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](https://docs.textcontrol.com/tx-spell-dotnet-wpf/examples/spell-checking/llms-full.txt) lists them.

### Next Steps

- [Other Controls](https://docs.textcontrol.com/tx-spell-dotnet-wpf/integration/other-controls/llms-full.txt): the same pattern for labels and other controls.
- [Events](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/events/llms-full.txt): the events of the spell checker and their arguments.
- [TX Text Control .NET for WPF](https://docs.textcontrol.com/tx-spell-dotnet-wpf/integration/tx-text-control/llms-full.txt): the integration that needs no code.

---

## Nearby

- **Previous:** [TX Text Control .NET for WPF](https://docs.textcontrol.com/tx-spell-dotnet-wpf/integration/tx-text-control/llms-full.txt)
- **Next:** [Other Controls](https://docs.textcontrol.com/tx-spell-dotnet-wpf/integration/other-controls/llms-full.txt)
