Lesson 2 - Manage the Spell Checker Programmatically

  • 5 minutes to read

This document shows how to integrate the SpellChecker into your WPF application, and provides step-by-step instructions on how to create a simple project that demonstrates the spell-check functionality.

Follow the steps below.

Step 1. Create a Project and Add Controls

This tutorial demonstrates how to start a spell check in a DevExpress TextEdit control when clicking a Button, although the SpellChecker can check the spelling of a wide range of text-aware controls.

  1. In MS Visual Studio, create a new WPF Application and open the MainWindow.xaml file in the Designer.
  2. Add a TextEdit object to your project by dragging the TextEdit item from the DX.20.1.WPF: Common Controls toolbox tab.

    Drop a Button control ("Check Spelling") from the All WPF Controls toolbox tab onto the window.

    DXSpellChecker_GettingStartedApp

  3. After this, your XAML should look like the following (if it does not, you can overwrite your code).

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
            Title="Spell Checker" Height="350" Width="525">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <dxe:TextEdit Name="textEdit1"
                          HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                          Grid.Column="1"
                          AcceptsReturn="True"
                          TextWrapping="Wrap"
                          VerticalContentAlignment="Top"/>
            <Button Name="button1"
                    Margin="12,12,12,0" Height="23" Width="85"
                    HorizontalAlignment="Left"  VerticalAlignment="Top"
                    Grid.Column="0"
                    Content="Check Spelling" Click="button1_Click" />
        </Grid>
    </Window>
    
  4. Add references to the following libraries:

    • DevExpress.Xpf.SpellChecker.v20.1.dll,
    • DevExpress.SpellChecker.v20.1.Core.dll.

    DXSpell Checker_AddReference

    NOTE

    Normally, when adding references to these assemblies, you should choose them from the Global Assembly Cache (GAC). However, if you prefer to copy them locally, or need to include them into your product's installation later, you can find their copies in the following directory.

    C:\Program Files (x86)\DevExpress 20.1\Components\Bin\Framework\

Step 2. Prepare a Dictionary

The next step is specifying a dictionary to perform the spell check.

In our tutorial, we will add the Polish OpenOffice dictionary. The advantages of OpenOffice dictionaries are that they are available for free, and are constantly updated, since they are a part of the open source project.

  1. On the Dictionaries page find the Polish (Poland) section, and download the Polish Dictionary Pack for OpenOffice 3.0.
  2. Rename the pack name extension to .zip and unpack it. You will get the pl_PL.dic file, containing a list of words, and the pl_PL.aff file, containing grammar rules.

Step 3. Add a Dictionary

  1. Create the Dictionaries folder in the directory of your project and copy the pl_PL.aff and pl_PL.dic files to it.
  2. Include the Dictionaries folder into the project.

    DXSpellChecker_IncludeDictionaries

    NOTE

    If the Dictionaries folder is not displayed in the Solution Explorer, select Show All Files from the Project menu or click Show All Files SpellChecker_ShowAllFilesIcon on the Solution Explorer toolbar.

  3. For these files, set the Build Action property to Embedded Resource.

    DXSpellChecker_EmbeddedResource

Step 4. Load the Dictionary and Check Spelling

Use the following code to load the Polish dictionary and start spell checking in the TextEdit control when an end user clicks the button:

using System.IO;
using System.Windows;
using System.Reflection;
using System.Globalization;
using DevExpress.Xpf.SpellChecker;
using DevExpress.XtraSpellChecker;

namespace WpfApplication1 {
    public partial class MainWindow : Window {
        SpellChecker checker;
        public MainWindow() {
            checker = new SpellChecker();

            InitializeComponent();

            // Obtain information about the Polish dictionary from the assembly.
            Stream dict = Assembly.GetExecutingAssembly().
                GetManifestResourceStream("WpfApplication1.Dictionaries.pl_PL.dic");
            Stream grammar = Assembly.GetExecutingAssembly().
                GetManifestResourceStream("WpfApplication1.Dictionaries.pl_PL.aff");

            // Create the dictionary and load information into it.
            SpellCheckerOpenOfficeDictionary dictionary = new SpellCheckerOpenOfficeDictionary();
            dictionary.LoadFromStream(dict, grammar, null);

            // Define the dictionary culture.
            CultureInfo culture = new CultureInfo("pl-PL");
            dictionary.Culture = culture;

            // Add the dictionary to the spell checker's collection.
            checker.Dictionaries.Add(dictionary);

            // Set the default culture of the spell checker.
            checker.Culture = culture;
        }

        private void button1_Click(object sender, RoutedEventArgs e) {
            // Start checking the text within a text box
            checker.Check(textEdit1);
        }
    }
}

Step 5. End-User Interaction

When SpellChecker encounters a word that is not found within available dictionaries, or a duplicated word, the Spelling dialog can be used for making corrections. It is invoked, when an end user starts checking spelling (calls the SpellChecker.Check method).

DXSpellChecker_GettingStarted_Result

In this dialog, an end user can replace the current occurrence or all occurrences of the misspelled word with one of the suggested corrections, ignore this word once, or whenever it occurs, or add the word to the dictionary of the SpellChecker (if the custom dictionary is added to the SpellChecker).

The Options... button enables an end user to change the spelling options or the language dictionary to be used, and provides access to the custom dictionary editor. The Undo Last button can cancel the changes, step by step.