TX Spell .NET for WPF

Suggestion Controls

Topic

Suggestions are the words TX Spell .NET offers in place of a misspelled one. The user sees them in the list of the spell check dialog, at the top of the context menu, or in a control of your own that you fill. TX Spell .NET has no separate suggestion control to place in a window: you set up the lists of its dialog and menu, or you show suggestions in a standard ListBox. Each of these places is covered below, with what it shows and the properties that change it.

Where Suggestions Appear

PlaceWhat you setCovered in
Suggestions list of the spell check dialogSuggestionListThe Suggestion List of the Dialog
Suggestions Dictionary list of the dialogSuggestionDictionaryComboBoxThe Suggestions Dictionary List
Top of the context menuThe maxSuggestions parameter of GetMenuItemsSuggestions in the Context Menu
A control of your ownCreateSuggestions or the SpellingCorrectionUIProviderShow Suggestions Beside the Editor

Which suggestions are found at all depends on the dictionaries: only a dictionary whose IsGetSuggestionsEnabled property is true provides suggestions. It is true by default, and the user changes it in the Suggestions column of the Options Dialog.

The Suggestion List of the Dialog

The spell check dialog lists the suggestions for the current word below the sentence. The SuggestionList property of the SpellCheckDialog describes this list:

The Spelling dialog over a RichTextBox with the misspelled word "sampke" and the suggestions sample, spake, smoke and samples in the Suggestions list.

PropertyDefaultEffect
MaxSuggestions10The largest number of suggestions in the list
IsNoSuggestionsItemEnabledtrueWhen no suggestion is found, the list shows an item that says so

Set them before the dialog opens, for example in the constructor of the window, where txSpellChecker1 is the spell checker taken from the resources:

SpellCheckDialog dialog = txSpellChecker1.SpellCheckDialog;

// Show the five best suggestions only
dialog.SuggestionList.MaxSuggestions = 5;

// Offer "All" in the Suggestions Dictionary list
dialog.SuggestionDictionaryComboBox.IsAllItemEnabled = true;

The second setting belongs to the list below the suggestions, described next.

The Suggestions Dictionary List

Suggestions Dictionary chooses the dictionary the suggestions come from. With one dictionary, it shows that dictionary. With several, for example English and a dictionary of medical terms, the user switches between them, and the list of suggestions changes with the choice. The picture shows the list in TX Text Control Words, a Windows Forms application:

The Spelling dialog over TX Text Control Words with the Suggestions Dictionary list open, offering en_US.dic and Shared Medical Dictionary.

The SuggestionDictionaryComboBox property of the SpellCheckDialog describes this list:

PropertyDefaultEffect
IsAllItemEnabledfalseAdds an All item that takes suggestions from all dictionaries at once. It appears only when more than one dictionary provides suggestions
IsAllItemSelectedfalseWhether All is selected
SuggestionDictionaryThe selected Dictionary, or null when All is selected

The list shows a dictionary by its file name, such as en_US.dic, unless you give it a DisplayName. The picture shows "Shared Medical Dictionary" as the display name of a second dictionary. Options Dialog sets one in code.

To open the dialog with suggestions from one dictionary, pass it to Show, as in Show(textControl1, dictionary).

Suggestions in the Context Menu

The context menu of a misspelled word starts with its suggestions. When you build the menu yourself with GetMenuItems, its last parameter is the largest number of suggestions:

The spell check menu of a TextControl with the suggestions sample, spake, smoke, samples and sampler at the top.

The Context menu strip samples of the setup pass 5. When no suggestion is found, the menu shows an item of the type NoSuggestions in their place. Context Menus builds the whole menu.

Show Suggestions Beside the Editor

A list next to the editor can show the suggestions for the word at the input position while the user types, without a right-click. A double-click on a suggestion replaces the word. In a new WPF project with the TX Text Control and TX Spell .NET packages, replace the contents of MainWindow.xaml, and change SpellingApp to the namespace of your project:

<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="Suggestions" Height="450" Width="800">
    <Window.Resources>
        <spell:TXSpellChecker x:Key="txSpellChecker1" />
    </Window.Resources>
    <DockPanel>
        <ListBox Name="suggestionList" DockPanel.Dock="Right" Width="180"
                 MouseDoubleClick="SuggestionList_MouseDoubleClick" />
        <tx:TextControl Name="textControl1"
                        IsSpellCheckingEnabled="True"
                        SpellChecker="txSpellChecker1"
                        InputPositionChanged="TextControl1_InputPositionChanged" />
    </DockPanel>
</Window>

Then replace the contents of MainWindow.xaml.cs:

using System;
using System.Windows;
using System.Windows.Input;
using TXTextControl;
using TXTextControl.Proofing;
using TXTextControl.WPF.Proofing;

namespace SpellingApp
{
    public partial class MainWindow : Window
    {
        private readonly TXSpellChecker txSpellChecker1;

        public MainWindow()
        {
            InitializeComponent();
            txSpellChecker1 = (TXSpellChecker)FindResource("txSpellChecker1");
        }

        private void TextControl1_InputPositionChanged(object sender, EventArgs e)
        {
            suggestionList.Items.Clear();

            // The misspelled word at the input position
            MisspelledWord word = textControl1.MisspelledWords.GetItem();
            if (word == null || textControl1.Selection.Start < word.Start - 1
                || textControl1.Selection.Start > word.Start + word.Length - 1)
            {
                return;
            }

            txSpellChecker1.CreateSuggestions(word.Text, 6);
            foreach (Suggestion suggestion in txSpellChecker1.Suggestions)
            {
                suggestionList.Items.Add(suggestion.Text);
            }
        }

        private void SuggestionList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (suggestionList.SelectedItem == null) return;

            // Replace the misspelled word with the chosen suggestion
            textControl1.MisspelledWords.Remove(textControl1.MisspelledWords.GetItem(), suggestionList.SelectedItem.ToString());
            textControl1.Focus();
        }
    }
}

Run the application and type a misspelled word: its suggestions appear in the list on the right.

The code works as follows:

  • The InputPositionChanged event of the TextControl updates the list whenever the input position moves.
  • GetItem without a parameter returns the misspelled word at the input position from the MisspelledWords collection, or the next one when there is none. The comparison with Selection.Start leaves the list empty when the input position is not in the word or directly in front of or behind it. Selection.Start counts from 0 and Start of the word from 1.
  • CreateSuggestions creates at most six suggestions for the word and stores them in Suggestions, a collection of Suggestion objects with a Text.
  • Remove of the MisspelledWords collection replaces the word with the chosen suggestion.

Incorrect Words and Suggestions explains CreateSuggestions and its overloads in detail.

Bind a List to the Correction Provider

For a correction panel that works through the misspelled words one by one, like the dialog, bind a ListBox to the SpellingCorrectionUIProvider in XAML. Its Suggestions always hold the suggestions for the current word, and its SuggestionDictionariesHandling fills a list of dictionaries like the one of the dialog. The list updates through data binding, without event handlers of yours. Spelling Correction Modes and UI Provider builds such a panel.

Next Steps