Spell Checker

This documents describes how to configure spell checking with the required dictionaries, add or remove words from a custom dictionary, disable spell checking and more.

By default, spell checking is enabled and configured to use the en-US dictionary. Spell checker reviews the text in all text fields, as well as text areas, on the loaded web page and highlights all misspelled words. To access and configure the spell checker, use the ISpellChecker interface:

ISpellChecker spellChecker = engine.SpellChecker;

Dictionaries

The spell checker supports multiple dictionaries and can check the text in different languages on a single web page. To configure the spell checker with the required dictionaries, use the ISpellChecker.DictionaryNames property. Chromium downloads the dictionary files automatically from its web servers.

For example:

spellChecker.DictionaryNames = new List<string>{"en-US", "de"};

The code sample above configures the spell checker with German de and English en-US dictionaries.

Custom dictionary

The spell checker supports the custom dictionary. You can access a custom dictionary using the SpellChecker.CustomDictionary property.

For example:

ISpellCheckDictionary dictionary = spellChecker.CustomDictionary;

Adding a word

You can add words to the custom dictionary using the Add(string) method:

bool success = dictionary.Add("John");

DotNetBrowser has the following requirements for the words added to the custom dictionary: - UTF-8 word encoding; - word length between 1 and 99 bytes; - no leading or trailing ASCII whitespace.

Removing a word

To remove a word from the custom dictionary, use the Remove(string) method as shown in the code sample below:

bool success = dictionary.Remove("John");

Disabling spell checking

By default, spell checking is enabled. You can disable it using the code sample below:

spellChecker.Enabled = false;

Context menu

You can display context menu with suggestions when a user right clicks the highlighted misspelled word on the loaded web page. Use the IBrowser.ShowContextMenuHandler to display context menu with suggestions and menu item to add the misspelled word to the custom dictionary.

WinForms

The code sample below demonstrates how to create and display WinForms context menu with suggestions and the Add to Dictionary menu item. Using this context menu, you can replace the misspelled word with one of the suggestions or add it to the custom dictionary.

browser.ShowContextMenuHandler = 
    new AsyncHandler<ShowContextMenuParameters, ShowContextMenuResponse>(ShowMenu);
private Task<ShowContextMenuResponse> ShowMenu(ShowContextMenuParameters parameters)
{
    TaskCompletionSource<ShowContextMenuResponse> tcs = 
        new TaskCompletionSource<ShowContextMenuResponse>();

    SpellCheckMenu spellCheckMenu = parameters.SpellCheckMenu;
    if (spellCheckMenu != null)
    {
        BeginInvoke(new Action(() =>
        {
            ContextMenu popupMenu = new ContextMenu();
            IEnumerable<string> suggestions = spellCheckMenu.DictionarySuggestions;
            if (suggestions != null)
            {
                foreach (string suggestion in suggestions)
                {
                    popupMenu.MenuItems.Add(BuildMenuItem(suggestion, true, delegate
                    {
                        browser.ReplaceMisspelledWord(suggestion);
                        tcs.TrySetResult(ShowContextMenuResponse.Close());
                    }));
                }
            }

            string addToDictionary = 
                spellCheckMenu.AddToDictionaryMenuItemText ?? "Add to Dictionary";

            popupMenu.MenuItems.Add(BuildMenuItem(addToDictionary, true, delegate
            {
                engine.SpellChecker?.CustomDictionary?.Add(spellCheckMenu.MisspelledWord);
                tcs.TrySetResult(ShowContextMenuResponse.Close());
            }));

            popupMenu.Collapse += (sender, args) =>
            {
                tcs.TrySetResult(ShowContextMenuResponse.Close());
            };

            System.Drawing.Point location = 
                new System.Drawing.Point(parameters.Location.X, parameters.Location.Y);
            popupMenu.Show(this, location);
            tcs.TrySetResult(ShowContextMenuResponse.Close());
        }));
    }
    else
    {
        tcs.TrySetResult(ShowContextMenuResponse.Close());
    }
    return tcs.Task;
}

private MenuItem BuildMenuItem(string item, bool isEnabled,
                               EventHandler clickHandler)
{
    MenuItem result = new MenuItem();
    result.Text = item;
    result.Enabled = isEnabled;
    result.Click += clickHandler;

    return result;
}

If you right click a misspelled word, the context menu appears: WinForms Spell Checker Context Menu

WPF

The code sample below demonstrates how to create and display a WPF context menu with suggestions and the Add to Dictionary menu item. Using this context menu, you can replace the misspelled word with one of the suggestions or add it to the custom dictionary.

browser.ShowContextMenuHandler = 
    new AsyncHandler<ShowContextMenuParameters, ShowContextMenuResponse>(ShowMenu);
private Task<ShowContextMenuResponse> ShowMenu(ShowContextMenuParameters parameters)
{
    TaskCompletionSource<ShowContextMenuResponse> tcs = 
        new TaskCompletionSource<ShowContextMenuResponse>();

    SpellCheckMenu spellCheckMenu = parameters.SpellCheckMenu;
    if(spellCheckMenu != null)
    {
        Dispatcher?.BeginInvoke(new Action(() =>
        {            
            ContextMenu popupMenu = new ContextMenu();
            IEnumerable<string> suggestions = spellCheckMenu.DictionarySuggestions;
            if (suggestions != null)
            {
                foreach(string suggestion in suggestions)
                {
                    popupMenu.Items.Add(BuildMenuItem(suggestion, true, delegate
                    {
                        browser.ReplaceMisspelledWord(suggestion);
                        tcs.TrySetResult(ShowContextMenuResponse.Close());
                    }));
                }             
            }
            
            string addToDictionary = 
                spellCheckMenu.AddToDictionaryMenuItemText ?? "Add to Dictionary";

            popupMenu.Items.Add(BuildMenuItem(addToDictionary, true, delegate
            {
                engine.SpellChecker?.CustomDictionary?.Add(spellCheckMenu.MisspelledWord);
                tcs.TrySetResult(ShowContextMenuResponse.Close());
            }));

            popupMenu.Closed += (sender, args) => 
            { 
                tcs.TrySetResult(ShowContextMenuResponse.Close()); 
            };

            popupMenu.IsOpen = true;
        }));
    } 
    else
    {
        tcs.TrySetResult(ShowContextMenuResponse.Close());
    }
    return tcs.Task;
}

private MenuItem BuildMenuItem(string item, bool isEnabled,
                               RoutedEventHandler clickHandler)
{
    MenuItem result = new MenuItem();
    result.Header = item;
    result.IsEnabled = isEnabled;
    result.Click += clickHandler;

    return result;
}

If you right click a misspelled word, the context menu appears: WPF Spell Checker Context Menu

Spell checker events

When a text field or text area on the loaded web page is in focus, the spell checker automatically checks the text and highlights the misspelled words. To get notifications when the text has been checked, use the IBrowser.SpellCheckCompleted event as shown in the code sample below:

browser.SpellCheckCompleted += (s, e) => 
{
    // The text that has been checked.
    string text = e.CheckedText;
    // The list of the spell checking results.
    foreach(SpellCheckingResult spellCheckingResult in e.Results)
    {
        // The location of the first symbol in the mis-spelled word 
        // in the checked text that is considered as mis-spelled 
        // by the spell checker.
        uint location = spellCheckingResult.Location;
        // The length of the mis-spelled word in the checked text
        uint length = spellCheckingResult.Length;
    }
};
Go Top