Spell Checking Simple WPF TextBoxes

TX Spell .NET can be used to spell check any text. It has been designed to be effortlessly integrated into any kind of control. This article describes how to use TX Spell .NET with .NET TextBoxes in different ways.

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

  • Samples\WPF\CSharp\Check simple text
  • Samples\WPF\VB.NET\Check simple text

Relevant API Links

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

The first group box consists of a simple 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 &gt; 0)
{
    tbDemo1.SelectAll();
    MessageBox.Show("The text box contains misspelled words.",
        "TX Spell .NET Demo",
        MessageBoxButton.OK,
        MessageBoxImage.Error);
}
else
    MessageBox.Show("Spelling is correct.",
        "TX Spell .NET Demo",
        MessageBoxButton.OK,
        MessageBoxImage.Information);
txSpellChecker1.Check(tbDemo1.Text)

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

The second group box consists only of a TextBox. Type some text into the TextBox to start the validation process. TX Spell .NET is used to validate the text by checking the spelling.

Image

The result of this sample is very similar to the first group, but it uses a ValidationRule that uses TX Spell .NET to validate the spelling. It returns false, if the spelling is not correct. The text box is validated everytime the TextBox content is changed.

public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
    TXSpell.Check((string)value);

    if (TXSpell.IncorrectWords.Count &gt; 0)
    {
        return new ValidationResult(false, "Misspelled.");
    }

    return new ValidationResult(true, null);
}
Public Overrides Function Validate(ByVal value As Object, _
    ByVal cultureInfo As Globalization.CultureInfo) As ValidationResult
    TXSpell.Check(DirectCast(value, String))

    If TXSpell.IncorrectWords.Count &gt; 0 Then
        Return New ValidationResult(False, "Misspelled.")
    End If

    Return New ValidationResult(True, Nothing)
End Function

The validation rule is added to the TextBox using a binding path in the XAML view:

<TextBox x:Name="Box" Validation.ErrorTemplate="{StaticResource TextBoxErrorTemplate}" Margin="9,40,12,58" Height="23">
    <TextBox.Text>
        <Binding Path="ValueInBox" UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <CheckSimpleText:SpellingRule x:Name="spellingRule1"></CheckSimpleText:SpellingRule>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>