TX Spell .NET for Windows Forms

Check Spelling as You Type

Topic

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.

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 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:

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 initializes the spell checker. A TextControl does this when you connect it; with any other control, call it once before the first check.
  • Check 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 holds the incorrect words the last Check found. Each IncorrectWord has a Start, which counts from 0 as RichTextBox.Select does, and a Length.
  • 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.

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.

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:

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."

Next Steps