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
- Step 2. Add Dictionaries
- Step 3. Check Spelling On Demand
- Step 4. Check Spelling As You Type
- Step 5. User Interaction
Step 1. Add a SpellChecker Component and the MemoEdit Control
Add the SpellChecker component to your project. To do this, simply drag the corresponding item from the DX.24.2: Components toolbox tab, and drop it onto the form.
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.
Specify the default language (German) of the spell checker. For this, set SpellCheckerBase.Culture property to de-DE using the Property editor:
Add the MemoEdit control (located on the DX.24.2: 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.
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:
- SpellCheckerDictionary that is just a simple list of words;
- SpellCheckerISpellDictionary that originates from the ISpell project;
- SpellCheckerOpenOfficeDictionary that is part of the OpenOffice.org project;
- HunspellDictionary that originates from the HunSpell project;
- SpellCheckerCustomDictionary that can be extended by user additions.
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.
- 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.
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
- 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.
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 on the Solution Explorer toolbar.
- 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.
- Open the Properties window for the SpellChecker component, and click the Dictionaries item. The ISpellCheckerDictionary Collection Editor window will be invoked.
- In this window, click the Add arrow, and select SpellCheckerOpenOfficeDictionary to add the OpenOffice dictionary.
Specify the dictionary properties as follows:
- Culture - de-DE
- DictionaryPath - Dictionaries\de_DE.dic
- Encoding - Western European (Windows)
- GrammarPath - Dictionaries\de_DE.aff
- To add the custom dictionary, click the Add arrow and select the SpellCheckerCustomDictionary item.
Specify the dictionary properties as follows:
- AlphabetPath - Dictionaries\GermanAlphabet.txt
- Culture - de-DE
- DictionaryPath - Dictionaries\CustomGermanDictionary.txt
- Encoding - Western European (Windows)
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:
- Set the SpellCheckMode property of SpellChecker to OnDemand.
- Add the SimpleButton control (located on the DX.24.2: Common Controls toolbox tab) to the form.
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:
- Set the SpellCheckMode property of SpellChecker to AsYouType.
To perform spell check as you type, use the following code:
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.
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.
You can, however, change it to the more versatile SpellingFormType.Word, shown in the picture below, using the SpellCheckerBase.SpellingFormType property.
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.