Skip to main content

Lesson 2 - Create Simple Application to Check Spelling in MemoEdit

  • 6 minutes to read

This tutorial demonstrates how to use the SpellChecker component to check spelling in DevExpress MemoEdit control using German dictionaries.

The SpellChecker component can check the spelling of a wide range of text-aware controls. For more information, refer to Supported Controls.

To create an application, follow the steps below:

Step 1. Add a SpellChecker Component and the MemoEdit Control

  1. Add the SpellChecker component to your project. To do this, simply drag the corresponding item from the DX.24.1: Components toolbox tab, and drop it onto the form.

    spellchecker toolbox

    Note

    The SharedDictionaryStorage component item, situated in the toolbox near the SpellChecker component, may be added to the project, if it is necessary to have several dictionaries available for different instances of the SpellChecker component.

  2. Specify the default language (German) of the spell checker. For this, set SpellCheckerBase.Culture property to de-DE using the Property editor:

    getstarted_spellchecker_culture

  3. Add the MemoEdit control (located on the DX.24.1: Common Controls toolbox tab) to the form and open the Properties window for it (e.g. by pressing F4). Since the control’s content can be checked by the SpellChecker component, a number of extender properties appears when it is dropped on the form containing the SpellChecker.

    spellchecker-options

    For now, you may leave these settings as is, and go to the next step.

Step 2. Add Dictionaries

The next step is specifying the dictionaries.

Currently there are four types of dictionaries:

For detailed information on dictionaries, see Dictionaries.

For each dictionary, you should specify its Culture and Encoding properties. Complex dictionaries, such as ISpell, OpenOffice or HunSpell, require an affix file, located in the path specified by the GrammarPath property. The need for the alphabet file is due to the method used to generate suggestion lists. For more details, please refer to the SpellCheckerDictionaryBase.AlphabetPath topic.

You can find OpenOffice dictionary at the Dictionaries page of the OpenOffice.org project. Download the required dictionaries extension form the extension repository. Change file extension to zip and unzip it with any archiver. Extract the *.dic and *.aff files to a temporary folder.

In this example, an OpenOffice German dictionary and a custom dictionary is used.

  1. Create the Dictionaries folder in the directory of your project and copy the required dictionary files (de_DE.dic and de_DE.aff) to it.
  2. Create a new text file in the Dictionaries folder and name it GermanAlphabet.txt. This file will contain German letters in the order required for proper sorting. Copy the following string to that file:

    AÄBCDEFGHIJKLMNOÖPQRSßTUÜVWXYZ

  3. Create a new empty text file in the Dictionaries folder and name it CustomGermanDictionary.txt. This file will contain words added to a custom dictionary.
  4. Include the Dictionaries folder in the project.

    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.

  5. Set the Copy to Output Directory property to Copy always for each file in the Dictionaries folder except the CustomGermanDictionary.txt. For the CustomGermanDictionary.txt specify Copy if newer.
  6. Open the Properties window for the SpellChecker component, and click the Dictionaries item. The ISpellCheckerDictionary Collection Editor window will be invoked.
  7. In this window, click the Add arrow, and select SpellCheckerOpenOfficeDictionary to add the OpenOffice dictionary.
  8. Specify the dictionary properties as follows:

    • Culture - de-DE
    • DictionaryPath - Dictionaries\de_DE.dic
    • Encoding - Western European (Windows)
    • GrammarPath - Dictionaries\de_DE.aff

    openoffice_de_dictionaries

  9. To add the custom dictionary, click the Add arrow and select the SpellCheckerCustomDictionary item.
  10. Specify the dictionary properties as follows:

    • AlphabetPath - Dictionaries\GermanAlphabet.txt
    • Culture - de-DE
    • DictionaryPath - Dictionaries\CustomGermanDictionary.txt
    • Encoding - Western European (Windows)

    openoffice_de_customdictionary

Now, decide whether SpellChecker will start spell check on demand (for example, when clicking a button), or it will check spelling as you type

Step 3. Check Spelling On Demand

To start checking the spelling in the MemoEdit control when clicking the button:

  1. Set the SpellCheckMode property of SpellChecker to OnDemand.
  2. Add the SimpleButton control (located on the DX.24.1: Common Controls toolbox tab) to the form.
  3. To start spell checking in the MemoEdit control and invoke the Spelling dialog to make corrections when you click the button, use the following code:

    private void simpleButton1_Click(object sender, EventArgs e) {
        spellChecker1.Check(memoEdit1);
    }
    

    When this method is called at runtime, the SpellChecker component loads the dictionaries, if they are not already loaded, and starts spell checking the MemoEdit control’s content.

    Note

    You may use the SpellChecker.SetShowSpellCheckMenu method to show the Check Spelling item in the MemoEdit control’s context menu.

Step 4. Check Spelling As You Type

To enable SpellChecker to check the spelling of the MemoEditor control as you type:

  1. Set the SpellCheckMode property of SpellChecker to AsYouType.
  2. To perform spell check as you type, use the following code:

    View Example

    private void Form1_Load(object sender, EventArgs e) {
        spellChecker1.SpellCheckMode = SpellCheckMode.AsYouType;
        spellChecker1.Check(memoEdit1);
    }
    

    This underlines the misspelled word when the end-user finishes typing the word or moves the cursor after changing it. The user can then right-click the word and select a suggestion from the context menu.

    getstarted_check_de

Step 5. 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 the end-user starts checking spelling on demand (calls the SpellChecker.Check method), or selects the Check Spelling item from the context menu.

Generally, the Spelling dialog of the SpellingFormType.Outlook type will best suit the user.

getstarted_check_outlookform_de

You can, however, change it to the more versatile SpellingFormType.Word, shown in the picture below, using the SpellCheckerBase.SpellingFormType property.

getstarted_check_wordform_de

In this dialog, the 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 all its occurrences, or add the word to the dictionary of the SpellChecker.

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