Lesson 1 - Add Spell Checking to Your Application
- 3 minutes to read
This topic will show how to add spell-check functionality to your application, using the SpellChecker component. Dictionaries for US English are built-in and will be used by default.
Note
If no dictionary is specified, SpellChecker uses default English dictionary. When any other dictionary is added, even a custom dictionary, default dictionary is not loaded. Note that the default English dictionary is used when the machine’s current culture is EN-US.
The SpellChecker component can check the spelling of a wide range of text editors.
Please follow the steps below. We assume that your application’s form has several different text editors from the list of supported controls.
Step 1. Add a SpellChecker Component
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 that contains text controls.
Step 2. Add Code for Check Spelling On Demand
To start checking the spelling in all supported controls on the form when clicking the button:
- Add the SimpleButton control (located on the DX.24.1: Common Controls toolbox tab) to the form;
- Subscribe to the button’s Click event.
To start spell checking and invoke the spelling dialog to make corrections when you click the button, call the SpellChecker.CheckContainer method.
Step 3. Add Code for Check Spelling As You Type
To enable SpellChecker to check the spelling of all supported controls on the form as you type:
- Subscribe to the form’s Load event;
Add the following code to the Load event handler:
private void Form1_Load(object sender, EventArgs e) { // Set spell checking options. spellChecker1.SpellCheckMode = DevExpress.XtraSpellChecker.SpellCheckMode.AsYouType; spellChecker1.ParentContainer = this; spellChecker1.CheckAsYouTypeOptions.CheckControlsInParentContainer = true; // Set focus to the RichTextBox control to show red wavy lines under misspelled words. richTextBox1.Select(); }
This code instructs the SpellChecker to underline the misspelled word when the end-user finishes typing the word or moves the cursor after changing it. The user can then right-click a word and select a suggestion from the context menu.
Note
SpellChecker relies on an application message loop - selection and tracking text changes are performed on Application.Idle event. If the event isn’t raised, you may experience issues with SpellChecker (for example, underlines are not be updated). Raise the Idle
event manually to overcome this behavior. An optimal solution is to call the `Application.RaiseIdle`` method on a timer.
Step 4. Run the project
The misspelled words are underlines in the text control which has the focus. The RichEditControl always display underlines whether it has focus or not, as illustrated in the following picture:
Click the Check Spelling button to start spelling check for all controls on the form and invoke the Spelling Dialog.