# Check Spelling as You Type

> Color misspelled words red in a Windows Forms RichTextBox as users type with TXSpellChecker.Check and IncorrectWords, and check a TextBox in Validating.

- **Product:** TX Spell .NET for Windows Forms
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Spell .NET for Windows Forms > Getting Started > RichTextBox and TextBox > Check Spelling as You Type
- **2239 words**
- **Web URL:** https://docs.textcontrol.com/tx-spell-dotnet-winforms/getting-started/richtextbox/as-you-type/
- **LLMs URL:** https://docs.textcontrol.com/tx-spell-dotnet-winforms/getting-started/richtextbox/as-you-type/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-spell-dotnet-winforms/getting-started/richtextbox/as-you-type/llms-full.txt

---

TX Spell .NET doesn't need TX Text Control. Its spell checker checks any text you pass to it, so the standard `RichTextBox` of Windows Forms can mark misspelled words while the user types. You build a small application that does this: a new project with the TX Spell .NET package, a `RichTextBox`, a `TXSpellChecker`, and one event handler that colors every misspelled word red.

### Prerequisites

- Visual Studio on Windows with .NET 8 or higher.
- The setup of TX Spell .NET for Windows Forms 11.0, the full or the trial version, installed on your machine. It provides the package source **Text Control Offline Packages**. See [Installing the Product](https://docs.textcontrol.com/tx-spell-dotnet-winforms/installation/setup/llms-full.txt).

### Create the Project

1. Open Visual Studio and create a new project.
2. Select **C#** from the **Languages** list, **Windows** from the **Platform** list and **Desktop** as the **Project Type**.
3. Select the project template **Windows Forms App** and click **Next**.
4. Enter a project name and click **Next**.
5. Select .NET 8 or higher as the **Framework** and click **Create**.

### Install the NuGet Package

1. Select the project in **Solution Explorer** and choose **Project** > **Manage NuGet Packages...**.
2. Select **Text Control Offline Packages** in the **Package source** list and click **Browse**.
3. Select `TXTextControl.TXSpell.WinForms.SDK` and click **Install**.

This one package is all you need: it contains the spell checker component with its dialogs and menus, and your license. The package of TX Text Control is not required. [Package Overview](https://docs.textcontrol.com/tx-spell-dotnet-winforms/installation/nuget/packages/llms-full.txt) lists what the packages contain.

### Add the Controls

1. Open **Form1** in the designer.
2. From the **Common Controls** tab of the **Toolbox**, draw a `RichTextBox` onto the form. In the **Properties** window, set its **Dock** property to **Fill**. Its name is *richTextBox1*.
3. Open the **TXSpell 11.0** tab of the **Toolbox** and drag a `TXSpellChecker` onto the form. The component has no user interface: it appears as *txSpellChecker1* in the component tray below the form.
4. Select *richTextBox1*, click the **Events** button of the **Properties** window, and double-click the `TextChanged` event. Visual Studio creates the handler `richTextBox1_TextChanged` in *Form1.cs*.

### Write the Code

Replace the class in *Form1.cs* with the following code, and keep the `namespace` that Visual Studio created around it:

```csharp
using System;
using System.Drawing;
using System.Windows.Forms;
using TXTextControl.Proofing;

public partial class Form1 : Form
{
    // Set while the handler colors words, which changes the RichTextBox itself
    private bool isColoring;

    public Form1()
    {
        InitializeComponent();

        // Without a connected TextControl, initialize the spell checker yourself
        txSpellChecker1.Create();
    }

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
        if (isColoring) return;
        isColoring = true;

        int caret = richTextBox1.SelectionStart;

        // Start with all text black, so that corrected words lose their color
        richTextBox1.SelectAll();
        richTextBox1.SelectionColor = Color.Black;

        // Check the whole text and color each incorrect word red
        txSpellChecker1.Check(richTextBox1.Text);

        foreach (IncorrectWord incorrectWord in txSpellChecker1.IncorrectWords)
        {
            richTextBox1.Select(incorrectWord.Start, incorrectWord.Length);
            richTextBox1.SelectionColor = Color.Red;
        }

        // Put the caret back where the user was typing
        richTextBox1.Select(caret, 0);
        isColoring = false;
    }
}
```

#### What the Code Does

- [Create](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/create-method/llms-full.txt) initializes the spell checker. A `TextControl` does this when you connect it; with any other control, call it once before the first check.
- [Check](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/check-method/llms-full.txt) checks the text against the dictionaries of the spell checker. The English dictionary is installed with TX Spell .NET, so English text is checked without further setup.
- [IncorrectWords](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/incorrectwords-property/llms-full.txt) holds the incorrect words the last `Check` found. Each [IncorrectWord](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/incorrectword/llms-full.txt) has a [Start](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/spelledword/start-property/llms-full.txt), which counts from 0 as `RichTextBox.Select` does, and a [Length](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/spelledword/length-property/llms-full.txt).
- Selecting a word and setting `SelectionColor` colors it. Because this changes the control, the `isColoring` flag keeps the handler from starting again while it runs.
- The caret is restored at the end, so the user can keep typing where they were.

### Run the Application

Press **F5** and type a sentence with a spelling mistake, such as *This is a wroong word.* The misspelled word turns red; correct it, and it turns black again. The *RichTextBox\\Multiple controls* sample of the setup checks the text the same way, with several `RichTextBox` controls that share one spell checker, and colors the misspelled word at the caret:

![The Multiple Controls sample with its RichTextBox controls; the misspelled word "wroong" is red in three of them.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-winforms/richtextbox-multi-controls.22606909.webp)

The handler checks the whole text on every change. A word is therefore red while it is still incomplete, and the check takes longer as the text grows. TX Text Control, which underlines words once they are complete, does this for you; see [Create Your First Windows Forms Application](https://docs.textcontrol.com/tx-spell-dotnet-winforms/getting-started/tx-text-control/first-application/llms-full.txt).

### Check a TextBox When It Loses the Focus

A single-line `TextBox` is often checked once, when the user leaves it. Draw a `TextBox` called *textBox1* and a `Label` called *labelResult* onto the form, and handle the `Validating` event of the text box, which fires when it loses the focus:

```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;
    }
    else
    {
        labelResult.Text = "Correct.";
        labelResult.ForeColor = Color.Green;
    }
}
```

`IncorrectWords.Count` is 0 when every word is spelled correctly. The *Check simple text* sample of the setup shows the result below the text box:

![A form with two text boxes; below the lower one, a green label says "Correct."](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-winforms/textbox-validation-label.0e43fb03.webp)

### Next Steps

- [Open the Spell Check Dialog](https://docs.textcontrol.com/tx-spell-dotnet-winforms/getting-started/richtextbox/dialog/llms-full.txt): let the user correct the words of the `RichTextBox` in a dialog.
- [Incorrect Words and Suggestions](https://docs.textcontrol.com/tx-spell-dotnet-winforms/spell-checking/suggestions/llms-full.txt): create suggestions for the words `Check` found.
- [Checking Multiple Controls](https://docs.textcontrol.com/tx-spell-dotnet-winforms/spell-checking/multiple-controls/llms-full.txt): share one spell checker between several controls.

---

## Nearby

- **Previous:** [RichTextBox and TextBox](https://docs.textcontrol.com/tx-spell-dotnet-winforms/getting-started/richtextbox/llms-full.txt)
- **Next:** [Open the Spell Check Dialog](https://docs.textcontrol.com/tx-spell-dotnet-winforms/getting-started/richtextbox/dialog/llms-full.txt)
