TX Spell .NET for WPF

Context Menus

Topic

Most users correct a misspelled word without leaving the text: they right-click the underlined word and choose the right spelling from a menu. With TX Text Control, TX Spell .NET provides this menu without any code. When you need other items in it, or a menu for another control, you declare your own ContextMenu and fill it with ready-made menu items. The sections below cover the built-in menu, how to replace it and how to react to its items.

The Built-In Menu

When a TXSpellChecker is connected to a TextControl through its SpellChecker property and IsSpellCheckingEnabled is true, misspelled words get a red zigzag line, and a right-click on one of them opens the spell check menu of TX Text Control instead of the usual context menu:

Four TextControls in one window, with red underlines under misspelled words, and the context menu of the misspelled word "haeder" with suggestions, Ignore, Ignore All, Add to Dictionary, Spell Check Dialog and Options.

From the menu, the user:

  • replaces the word with one of the suggestions at the top;
  • ignores the word here or everywhere, with Ignore and Ignore All;
  • adds it to a user dictionary with Add to Dictionary;
  • opens the spell check dialog of TX Text Control with Spell Check Dialog...;
  • opens the options dialog with Options....

Add to Dictionary is enabled only when a user dictionary has been added to the dictionaries of the spell checker, as User Dictionaries shows. Correct Words from the Context Menu connects the spell checker step by step.

Choose Which Menu Appears

The SpellCheckContextMenu property of the TextControl decides what a right-click on a misspelled word opens. It is a string:

ValueMenu
(Default)The built-in menu shown above
(none)No menu
The key of a ContextMenu resourceYour own menu

A menu of your own must be a resource of the TextControl, or of an element that contains it, such as the window. This XAML declares a menu in the resources of the TextControl and selects it by its key:

<Window x:Class="SpellingApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:tx="clr-namespace:TXTextControl.WPF;assembly=TXTextControl.WPF"
        xmlns:spell="clr-namespace:TXTextControl.WPF.Proofing;assembly=TXSpell.WPF"
        Title="Spelling" Height="450" Width="800">
    <Window.Resources>
        <spell:TXSpellChecker x:Key="txSpellChecker1" />
    </Window.Resources>
    <tx:TextControl Name="textControl1"
                    IsSpellCheckingEnabled="True"
                    SpellChecker="txSpellChecker1"
                    SpellCheckContextMenu="UserSpellCheckMenu">
        <tx:TextControl.Resources>
            <ContextMenu x:Key="UserSpellCheckMenu">
                <MenuItem Header="Spell Check Dialog..." Click="SpellCheckDialog_Click" />
                <MenuItem Header="Options..." Click="Options_Click" />
            </ContextMenu>
        </tx:TextControl.Resources>
    </tx:TextControl>
</Window>

The two items open the dialogs of TX Spell .NET. Their handlers go into the code-behind, where txSpellChecker1 is the spell checker taken from the resources with FindResource, as As-You-Type Checking shows:

private void SpellCheckDialog_Click(object sender, RoutedEventArgs e)
{
    txSpellChecker1.SpellCheckDialog.Show(textControl1);
}

private void Options_Click(object sender, RoutedEventArgs e)
{
    txSpellChecker1.OptionsDialog();
}

SpellCheckDialog.Show opens the Spell Check Dialog and OptionsDialog the Options Dialog.

Add the Spelling Items

A menu of your own also needs the suggestions for the word and the commands that correct it. GetMenuItems creates them for a misspelled word: its suggestions, Ignore, Ignore All and Add To Dictionary. Each item carries out its action when it is clicked, so you only add the items to the menu. The TextControl\Context menu strip sample of the setup builds its menu this way:

The spell check menu of the Context menu strip sample over the misspelled word "sampke": the suggestions sample, spake, smoke, samples and sampler, then Ignore, Ignore All and a disabled Add To Dictionary, then Spell Check Dialog and Options.

For a misspelled word of the TextControl, a MisspelledWord, the call takes the word, the TextControl and the largest number of suggestions:

TXTextControl.WPF.Proofing.MenuItem[] items = txSpellChecker1.GetMenuItems(misspelledWord, textControl1, 5);

It returns MenuItem objects, which are WPF MenuItem objects, so you add them to the Items of the ContextMenu together with items of your own. GetMenuItems doesn't create Spell Check Dialog... and Options...; those are items like the two in the XAML above. The sample of the setup shows the complete menu; Spell Checking Examples lists it.

What the Items Do

The Type of each MenuItem is a value of TXSpell.MenuItemType, and it decides what a click does:

TypeWhat a click does
SuggestionReplaces the misspelled word with the text of the item
IgnoreOnceIgnores this occurrence of the word
IgnoreAllIgnores every occurrence of the word
AddToDictionaryAdds the word to all editable user dictionaries
DeleteRemoves the word from the text
NoSuggestionsNothing; the item says that no suggestions were found

After each click, the MenuItemClicked event of the TXSpellChecker reports the item, for example to log words that users add:

txSpellChecker1.MenuItemClicked += (sender, e) =>
{
    if (e.MenuItem.Type == TXSpell.MenuItemType.AddToDictionary)
    {
        statusText.Text = "Word added to the dictionary.";
    }
};

MenuItem of the MenuItemClickedEventArgs is the clicked item. statusText stands for a TextBlock of your window, and TXSpell needs using TXTextControl.Proofing;. Events lists the other events a correction raises.

A Menu for a RichTextBox

A RichTextBox or a TextBox has no built-in spell check menu, but GetMenuItems works for it, too. Its second overload takes an IncorrectWord, found by Check, and the text that contains it. For a RichTextBox, the text is the content of its document:

TXTextControl.WPF.Proofing.MenuItem[] items = txSpellChecker1.GetMenuItems(
    currentIncorrectWord,
    new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text,
    5);

Without a connected TextControl, the spell checker isn't initialized for you: call its Create method once before the first check, for example in the constructor of the window.

The spell check menu over the misspelled word "sampke" in a RichTextBox, with the suggestions sample, spake, smoke, samples and sampler, then Ignore, Ignore All, Add To Dictionary, Spell Check Dialog and Options.

The spell checker can't change the text of a RichTextBox itself. When the user picks a suggestion, the IncorrectWordChanging event fires, and your handler replaces the word in the control, as Checking on Demand shows for the dialog. The RichTextBox\Context menu strip sample of the setup shows the whole menu.

Next Steps