Spell Checking Simple Windows Forms TextBoxes

TX Spell .NET is the perfect addition for TX Text Control to add professional, high-performance and reliable spell checking to your applications. Furthermore, combined with TX Text Control version 25.0, TX Spell .NET enables a fast way to hyphenate documents, detect language scopes and create synonyms.

This getting started tutorial is the shortcut to use TX Spell .NET for Windows Forms within Microsoft Visual Studio.

The source code for all steps is contained in the following directories:

  • %USERPROFILE%\My Documents\TX Spell 7.0.NET for Windows Forms\Samples\WinForms\CSharp\TextControl\Simple
  • %USERPROFILE%\My Documents\TX Spell 7.0.NET for Windows Forms\Samples\WinForms\VB.NET\TextControl\Simple

Relevant API Links

The sample project consists of a form divided into 3 groups. Each group shows a different way of integrating spell checking into simple Windows Forms TextBoxes.

Group 1: Simple Spell Checking

The first group box consists of a TextBox and a Button. Type in some text and click the button to start the spell checking process.

Image

The TXSpell.Check method is simply called with the text of the TextBox. The TXSpell.IncorrectWords collection retrieves all misspelled words. If the IncorrectWords.Count property returns more than 0, some words of the text are misspelled.

txSpellChecker1.Check(tbDemo1.Text);

if (txSpellChecker1.IncorrectWords.Count > 0)
{
    tbDemo1.SelectAll();
    MessageBox.Show("The text box contains misspelled words.", "TX Spell .NET Demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
    MessageBox.Show("Spelling is correct.", "TX Spell .NET Demo", MessageBoxButtons.OK, MessageBoxIcon.Information);
txSpellChecker1.Check(tbDemo1.Text)

If txSpellChecker1.IncorrectWords.Count > 0 Then
    tbDemo1.SelectAll()
    MessageBox.Show("The text box contains misspelled words.", "TX Spell .NET Demo", MessageBoxButtons.OK, MessageBoxIcon.[Error])
Else
    MessageBox.Show("Spelling is correct.", "TX Spell .NET Demo", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If

Group 2: TextBox AutoComplete

The second group box consists only of a TextBox. Start typing a word that will be completed automatically based on the current dictionary.

Image

The AutoCompleteMode of the TextBox is set to Suggest and the AutoCompleteMode property to CustomSource. TX Spell .NET is used to fill the custom source with the appropriate suggestions.

private void tbDemo2_TextChanged(object sender, EventArgs e)
{
    if (acscAutoCompleteCollection == null)
        acscAutoCompleteCollection = new AutoCompleteStringCollection();

    txSpellChecker1.Check(tbDemo2.Text);


    if (txSpellChecker1.IncorrectWords.Count > 0)
    {
        txSpellChecker1.CreateSuggestions(txSpellChecker1.IncorrectWords[0].Text, 10);


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

        tbDemo2.AutoCompleteCustomSource = acscAutoCompleteCollection;
    }
}
Private Sub tbDemo2_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles tbDemo2.TextChanged
    If acscAutoCompleteCollection Is Nothing Then
        acscAutoCompleteCollection = New AutoCompleteStringCollection()
    End If

    TxSpellChecker1.Check(tbDemo2.Text)

    If TxSpellChecker1.IncorrectWords.Count > 0 Then
        TxSpellChecker1.CreateSuggestions(TxSpellChecker1.IncorrectWords(0).Text, 10)

        For Each suggestion As TXTextControl.Proofing.Suggestion In TxSpellChecker1.Suggestions
            acscAutoCompleteCollection.Add(suggestion.Text)
        Next

        tbDemo2.AutoCompleteCustomSource = acscAutoCompleteCollection
    End If
End Sub

After checking the text using the TXSpell.Check method, suggestions are created using TXSpell.CreateSuggestions. The number of suggestions is limited to 10 words per misspelled word. Finally, the text of each suggestion is added to the AutoCompleteCollection thats acts as the customer source for the auto completion process.

Group 3: TextBox Validation

The third group box consists only of a TextBox and another Button. Typing some text into the TextBox and click on OK.

Image

The result of this sample is very similar to the first group, but it uses the Validating and Validated events of the TextBox. The Validating event is fired everytime when the TextBox looses the focus. On this event, TX Spell .NET is used to check the text.

private void tbDemo3_Validating(object sender, CancelEventArgs e)
{
    txSpellChecker1.Check(tbDemo3.Text);

    if (txSpellChecker1.IncorrectWords.Count > 0)
    {
        label4.Text = "Misspelled.";
        label4.ForeColor = Color.Red;
        e.Cancel = true;
    }
}
Private Sub tbDemo3_Validating(sender As Object, e As CancelEventArgs)
    txSpellChecker1.Check(tbDemo3.Text)

    If txSpellChecker1.IncorrectWords.Count > 0 Then
        label4.Text = "Misspelled."
        label4.ForeColor = Color.Red
        e.Cancel = True
    End If
End Sub

If the text is misspelled, the following Validated event is canceled. If not, the Validated event will be fired.