Skip to main content Skip to footer

Spell-checking in WPF

The C1SpellChecker component provides easy and efficient spell-checking for your WPF applications. It has built-in dialogs, similar to Microsoft Word spell-checking, that allow users to ignore, change words and even add them to a custom user dictionary. The C1SpellChecker has a rich API for many different spelling scenarios:

  • Check a single word
  • Check a block of text
  • Check an entire control (TextBox, C1RichTextBox or any wrapper for a control that inherits the ISpellCheckableEditor)
  • Get spelling suggestions for a misspelled word
  • Auto-replace commonly misspelled words
  • Ignores certain words

The C1SpellChecker also supports checking against three dictionaries: a main dictionary, a custom dictionary and a user dictionary. The user dictionary is typically created by the user behind the scenes when they add words to the dictionary. These words are kept out of the main dictionary. Here’s how you load a dictionary from a Resources directory:


// load main dictionary  
c1SpellChecker.MainDictionary.Load(Application.GetResourceStream(new Uri("/" + new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + ";component/Resources/C1Spell_en-US.dct", UriKind.Relative)).Stream);  

ComponentOne provides dictionaries for over 20 common languages, plus a tool for creating a custom dictionary from a list of words (so if necessary you can build your own for another language!). You can find the full list of international dictionaries available for separate download on this page. The C1SpellChecker Dictionary Editor allows you to import an existing dictionary (DCT file), or create a dictionary from a raw list of words. It can be found installed alongside Studio for WPF @ C:\Program Files (x86)\ComponentOne\Studio for WPF\DictionaryEditor. C1SpellChecker_Dictionary_Editor

Spell-Checking Controls

To spell-check a standard TextBox control we use the CheckControlAsync method. The parameters let us specify the TextBox, whether or not we start from the cursor and the spell dialog. We can use the built-in spelling dialog or provide a custom one. The following code uses the standard dialog.


c1SpellChecker.CheckControlAsync(textBox1, false, new C1.WPF.SpellChecker.C1SpellDialog());  

TIP: Handle the LostFocus event on the TextBox control so that the selected text remains selected while the C1SpellDialog has focus.


// keep text selected while spell-checking dialog is open  
private void \_plainTextBox\_LostFocus(object sender, RoutedEventArgs e)  
{  
    e.Handled = true;  
}  

You can customize the window caption and localize the C1SpellDialog using the included resource files. You can provide a completely customized dialog by implementing the ISpellDialog interface. Examples are included in the samples below. C1SpellChecker_RTB Spell-checking a C1RichTextBox control is built into the C1RichTextBoxToolbar. After you initialize a C1SpellChecker you assign it to the C1RichTextBox.SpellChecker property. The as-you-type spell-checking is only supported in the C1RichTextBox control. If you need to spell-check any other control, you should provide a wrapper that implements the ISpellCheckableEditor interface. We provide a sample that checks a C1DataGrid control as an example.

Ignoring Words

During the spell-check users can opt to ignore any misspelled word just like in Microsoft Word. But you as the developer can pre-code certain words to ignore. Just add words to the IgnoreList collection.


// set up ignore list  
WordList il = c1SpellChecker.IgnoreList;  
il.Add("ComponentOne");  
il.Add("Silverlight");  

Conclusion

Download SpellCheckerSamples which includes code for spell-checking a TextBox, C1RichTextBox, C1DataGrid, custom Word-style dialogs, and batch checking.

ComponentOne Product Manager Greg Lutz

Greg Lutz

comments powered by Disqus