# RichTextBox and TextBox

> Spell check a Windows Forms RichTextBox or TextBox with TX Spell .NET: Select positions, Validating, AutoComplete suggestions, the dialog and GetMenuItems.

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

---

The `RichTextBox` and `TextBox` of Windows Forms can be spell checked with TX Spell .NET alone: the package TXTextControl.TXSpell.WinForms.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 standard control, TX Spell .NET finds the mistakes and your code shows and fixes them. The tutorials in [RichTextBox and TextBox](https://docs.textcontrol.com/tx-spell-dotnet-winforms/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 or TextBox |
|---|---|
| Initialize the spell checker | Your code calls [Create](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-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-winforms/reference/txtextcontrol-proofing/txspellchecker/check-method/llms-full.txt) with the text of the control fills [IncorrectWords](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/incorrectwords-property/llms-full.txt) |
| Mark the words | Your code colors or selects them; there is no wavy underline |
| Offer the dialog | [SpellCheckDialog](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/spellcheckdialog-property/llms-full.txt)`.Show()` walks the user through the words of the last check |
| Apply a correction | Your code changes the text in the [IncorrectWordChanging](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/incorrectwordchanging-event/llms-full.txt) event |
| Offer a context menu | [GetMenuItems](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/getmenuitems-method/llms-full.txt) creates the items; your code adds them to a `ContextMenuStrip` |

The positions fit the controls directly: the [Start](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/spelledword/start-property/llms-full.txt) of an [IncorrectWord](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/incorrectword/llms-full.txt) is the zero-based index of its first character in the checked text, the index that `RichTextBox.Select` and `TextBox.Select` expect. `richTextBox1.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 wroong is colored red.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-winforms/richtextbox-multi-controls.22606909.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-winforms/getting-started/richtextbox/as-you-type/llms-full.txt) builds this application.

A `TextBox` can't color single words. Check its text when it loses the focus, in its `Validating` event, and tell the user that it contains mistakes when `IncorrectWords.Count` is greater than 0:

![A form with a TextBox containing spellling, a Check spelling button and a message box reading The text box contains misspelled words.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-winforms/textbox-check-message.9ae6e420.webp)

To keep the user in the field until the spelling is correct, set `Cancel` of the `CancelEventArgs` to `true` when `Check` finds a mistake. The `TextBox` then keeps the focus, and its `Validated` event isn't raised:

```csharp
private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
    txSpellChecker1.Check(textBox1.Text);

    if (txSpellChecker1.IncorrectWords.Count > 0)
    {
        labelResult.Text = "Misspelled.";
        labelResult.ForeColor = Color.Red;

        // Keep the focus in the text box and skip the Validated event
        e.Cancel = true;
    }
}
```

The handler of [Check a TextBox When It Loses the Focus](https://docs.textcontrol.com/tx-spell-dotnet-winforms/getting-started/richtextbox/as-you-type/#check-a-textbox-when-it-loses-the-focus) reports the result only and lets the user move on.

### Complete Words in a TextBox

The suggestions of TX Spell .NET can serve as the word list of the AutoComplete feature of a `TextBox`. While the user types, the incomplete word is misspelled, and its suggestions are the words it could become:

![A form whose AutoComplete text box contains wor and shows a drop-down list with word, wore, work, worm, worn and wort.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-winforms/textbox-autocomplete.b3a06f77.webp)

Set `AutoCompleteMode` of the text box to `Suggest` and `AutoCompleteSource` to `CustomSource`, in the designer or in code, and fill its `AutoCompleteCustomSource` in the `TextChanged` event:

```csharp
private void textBox2_TextChanged(object sender, EventArgs e)
{
    txSpellChecker1.Check(textBox2.Text);

    if (txSpellChecker1.IncorrectWords.Count > 0)
    {
        // At most 10 suggestions for the first misspelled word
        txSpellChecker1.CreateSuggestions(txSpellChecker1.IncorrectWords[0].Text, 10);

        AutoCompleteStringCollection words = new AutoCompleteStringCollection();

        foreach (TXTextControl.Proofing.Suggestion suggestion in txSpellChecker1.Suggestions)
        {
            words.Add(suggestion.Text);
        }

        textBox2.AutoCompleteCustomSource = words;
    }
}
```

The code works as follows:

- `Check` finds the words of the text that aren't in a dictionary; the first one is the word to complete.
- [CreateSuggestions](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/createsuggestions-method/llms-full.txt) creates up to 10 suggestions for it and puts them into [Suggestions](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/suggestions-property/llms-full.txt).
- The [Text](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/suggestion/text-property/llms-full.txt) of each [Suggestion](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/suggestion/llms-full.txt) goes into an `AutoCompleteStringCollection`, which becomes the custom source of the drop-down list.

When the text is spelled correctly, the list stays as it is. [Incorrect Words and Suggestions](https://docs.textcontrol.com/tx-spell-dotnet-winforms/spell-checking/suggestions/llms-full.txt) explains the suggestion methods.

### Correct in the Dialog

The spell check dialog works with any text: call `Check` first, then `SpellCheckDialog.Show()`, and apply each correction to the control in the `IncorrectWordChanging` event:

![The Spelling dialog over a RichTextBox, with the misspelled word sampke, No language set, and the suggestions sample, spake, smoke, samples, sampler and sampled.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-winforms/richtextbox-spelling-dialog.b515f6c9.webp)

[Open the Spell Check Dialog](https://docs.textcontrol.com/tx-spell-dotnet-winforms/getting-started/richtextbox/dialog/llms-full.txt) shows the code and explains the handler.

### 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 control and the maximum number of suggestions, and returns [MenuItem](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/menuitem/llms-full.txt) objects with the suggestions and the commands **Ignore**, **Ignore All**, **Add To Dictionary**, **Spell Check Dialog** and **Options**:

![A context menu over the misspelled word sampke in a RichTextBox, with five suggestions followed by Ignore, Ignore All, Add To Dictionary, Spell Check Dialog and Options.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-winforms/richtextbox-context-menu.f03e53b3.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-winforms/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-winforms/richtextbox-multi-dialog.646d122f.webp)

[Checking Multiple Controls](https://docs.textcontrol.com/tx-spell-dotnet-winforms/spell-checking/multiple-controls/#check-several-richtextboxes-in-one-dialog) shows both techniques.

### Samples

The setup installs samples for each technique, in C# and Visual Basic, below *TX Spell 11.0.NET for Windows Forms\\Samples\\WinForms* in your *Documents* folder. [Spell Checking Examples](https://docs.textcontrol.com/tx-spell-dotnet-winforms/examples/spell-checking/llms-full.txt) lists them.

### Next Steps

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

---

## Nearby

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