TX Spell .NET for Windows Forms

Customizing the Spell Check Dialog

Topic

Your application may need a spelling dialog that differs from the default one: without Add To Dictionary because users must not change the dictionaries, with a button that ends the check, in the font of the application, or with an options dialog of your own. The spell check dialog of TX Spell .NET can be changed in all these ways, in code, before it opens. What follows shows each change, and a form that combines them.

How the Dialog Is Built

You don't create the dialog. The SpellCheckDialog property of the TXSpellChecker returns it, a SpellCheckDialog object that keeps its settings for every time the dialog is shown. Its buttons are in three collections, one for each group of the dialog:

CollectionPlace in the dialogDefault buttons
IgnoreButtonsRight of the sentenceIgnore Once, Ignore All, Add To Dictionary
ChangeButtonsRight of the suggestionsChange, Change All
OptionButtonsBelow the suggestions dictionaryOptions...

Each collection is a DialogButtonCollection of DialogButton objects. A button has a Text, an Enabled state, a Font and a Type, a value of TXSpell.ButtonType that decides what a click does. Spell Check Dialog lists the types of the default buttons.

Change the collections while the dialog is closed. Add, AddRange and Remove throw an exception when they are called while it is open. The form's constructor is a good place.

The Modify SpellCheckDialog Sample

The setup installs a sample that changes the dialog, in the Modify SpellCheckDialog folder of the Windows Forms samples. Its window has check boxes for the changes; the user sets them and opens the dialog to see the result:

The window of the Modify SpellCheckDialog sample with a text to check and the check boxes With Own Options Dialog and Show Add To Dictionary, next to the Spelling dialog it opens.

To see a change, close the dialog, change the settings and open it again. Spell Checking Examples lists this sample with the other samples of the setup.

Rename a Button

Set the Text of a button. The sample renames the options button when its check box is checked:

txSpellChecker1.SpellCheckDialog.OptionButtons[0].Text = checkBox1.Checked ? "My Options..." : "Options...";

The indexer of a collection counts from 0, in the order of the table above. The new text is used the next time the dialog opens.

Remove or Disable a Button

To take Add To Dictionary away from the user, find it by its type and remove it from its collection:

SpellCheckDialog dialog = txSpellChecker1.SpellCheckDialog;

foreach (DialogButton button in dialog.IgnoreButtons)
{
    if (button.Type == TXSpell.ButtonType.AddToDictionary)
    {
        dialog.IgnoreButtons.Remove(button);
        break;
    }
}

To keep the button visible but grayed out, set its Enabled property to false instead. Removing the Options... button from OptionButtons the same way leaves the user no way to open the options dialog from the spell check dialog.

Add a Button of Your Own

A button of the type User has no built-in behavior. You create it, add it to a collection and handle its click in the DialogButtonClicked event of the TXSpellChecker. This Stop button, added to the group of Change and Change All, ends the check:

private DialogButton stopButton;

private void AddStopButton()
{
    stopButton = new DialogButton(TXSpell.ButtonType.User);
    stopButton.Text = "Stop";
    txSpellChecker1.SpellCheckDialog.ChangeButtons.Add(stopButton);

    txSpellChecker1.DialogButtonClicked += txSpellChecker1_DialogButtonClicked;
}

private void txSpellChecker1_DialogButtonClicked(object sender, DialogButtonEventArgs e)
{
    if (e.DialogButton == stopButton)
    {
        // Close the dialog before all words are checked
        e.StateManager.Close();
    }
}

The code works as follows:

Change the Font and the Preview

The Font of the SpellCheckDialog is used by its labels, the preview, the two lists and the Cancel button. The other buttons have a font of their own. The Preview property describes the sentence at the top of the dialog:

PropertyDefaultEffect
SpellCheckDialog.FontControl.DefaultFontThe font of the labels, the preview, the lists and Cancel
Preview.IncorrectWordColorColor.RedThe color of the misspelled word in the sentence
Preview.EditabletrueWhether the user can edit the sentence

One more property concerns the editor behind the dialog. HideSelection decides whether the text selected in the TextControl stays highlighted while the dialog is open. Its default is false, and it has an effect only when the dialog checks a TextControl.

The two lists of the dialog, the suggestions and the suggestions dictionary, are described in Suggestion Controls.

Use Your Own Options Dialog

The Options... button raises the OptionsDialogOpening event before the options dialog of TX Spell .NET opens. Set Cancel of its arguments to true and show your own form instead; the sample's With Own Options Dialog check box does this. Options Dialog shows the code.

Put It Together

This form connects a spell checker to a TextControl and changes the dialog in its constructor: Add To Dictionary is removed, a Stop button is added, Change answers the Enter key, and the font, the color and the number of suggestions are changed. It creates its controls in code, so it runs in a new Windows Forms project with the TX Text Control and TX Spell .NET packages:

using System;
using System.Drawing;
using System.Windows.Forms;
using TXTextControl.Proofing;

public class SpellingForm : Form
{
    private readonly TXTextControl.TextControl textControl1 = new TXTextControl.TextControl();
    private readonly TXSpellChecker txSpellChecker1 = new TXSpellChecker();
    private readonly System.Windows.Forms.Button buttonSpelling = new System.Windows.Forms.Button();
    private DialogButton stopButton;

    public SpellingForm()
    {
        textControl1.Dock = DockStyle.Fill;
        textControl1.SpellChecker = txSpellChecker1;
        textControl1.IsSpellCheckingEnabled = true;

        buttonSpelling.Text = "Spelling...";
        buttonSpelling.Dock = DockStyle.Bottom;
        buttonSpelling.Click += (sender, e) => txSpellChecker1.SpellCheckDialog.Show(textControl1);

        Controls.Add(textControl1);
        Controls.Add(buttonSpelling);

        SpellCheckDialog dialog = txSpellChecker1.SpellCheckDialog;

        // Users may not change the dictionaries
        foreach (DialogButton button in dialog.IgnoreButtons)
        {
            if (button.Type == TXSpell.ButtonType.AddToDictionary)
            {
                dialog.IgnoreButtons.Remove(button);
                break;
            }
        }

        // A button that ends the check
        stopButton = new DialogButton(TXSpell.ButtonType.User);
        stopButton.Text = "Stop";
        dialog.ChangeButtons.Add(stopButton);
        txSpellChecker1.DialogButtonClicked += txSpellChecker1_DialogButtonClicked;

        // Enter clicks Change instead of Ignore Once
        dialog.AcceptButton = dialog.ChangeButtons[0];

        dialog.Font = new Font("Segoe UI", 10);
        dialog.Preview.IncorrectWordColor = Color.Blue;
        dialog.SuggestionList.MaxSuggestions = 5;
    }

    private void txSpellChecker1_DialogButtonClicked(object sender, DialogButtonEventArgs e)
    {
        if (e.DialogButton == stopButton)
        {
            e.StateManager.Close();
        }
    }

    [STAThread]
    private static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new SpellingForm());
    }
}

Run the application, type a few misspelled words and click Spelling.... The dialog opens without Add To Dictionary, with a Stop button in the group of Change and Change All, and the misspelled word appears in blue.

The code works as follows:

  • System.Windows.Forms.Button is written in full because the TXTextControl namespace has a Button class, too.
  • AcceptButton is the button that Enter clicks. It is Ignore Once by default; ChangeButtons[0] is Change.
  • MaxSuggestions limits the suggestion list to five words instead of ten.

Next Steps