Implement Standalone WPF Spell Checker in Code-Behind
- 4 minutes to read
This tutorial describes how to start a spell check in a DevExpress TextEdit control on a Button click.
Add Items on the Form
In Microsoft Visual Studio, create a new WPF Application and open the MainWindow.xaml file in the designer.
Drag the TextEdit item from the DX.24.2.WPF: Common Controls toolbox tab and a Button control (“Check Spelling”) from the All WPF Controls toolbox tab onto the window.
After this, your XAML should look as follows (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>
Add Library References
Add references to the following libraries:
- DevExpress.Xpf.SpellChecker.v24.2.dll,
- DevExpress.SpellChecker.v24.2.Core.dll.
If you prefer to copy assemblies locally, or need to include them into your product’s installation later, you can find their copies in the following directory: C:\Program Files\DevExpress 24.2\Components\Bin\Framework\
Add Dictionaries
The next step is to specify a dictionary to perform the spell check.
This tutorial uses the Polish OpenOffice dictionary. The OpenOffice dictionaries are available for free, and are constantly updated as a part of the open source project.
On the Dictionaries page find the Polish (Poland) section, and download the Polish Dictionary Pack for OpenOffice 3.0.
Rename the pack name extension to .zip and unpack it. The archive contains the pl_PL.dic file with a list of words, and the pl_PL.aff file with grammar rules.
Create the Dictionaries folder in the directory of your project and copy the pl_PL.aff and pl_PL.dic files there. Include the Dictionaries folder in the project, and make sure that you set the Build Action property to Embedded Resource for dictionary files.
Tip
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.
Load Dictionaries and Handle the Click Event
Use the following code to load the dictionary and start spell checking in the TextEdit control when a user clicks the button:
using System.IO;
using System.Windows;
using System.Reflection;
using System.Globalization;
using DevExpress.Xpf.SpellChecker;
using DevExpress.XtraSpellChecker;
SpellChecker checker;
public MainWindow() {
checker = new SpellChecker();
InitializeComponent();
// Obtain dictionary information from the assembly
Stream dict = Assembly.GetExecutingAssembly().
GetManifestResourceStream("WpfApplication1.Dictionaries.pl_PL.dic");
Stream grammar = Assembly.GetExecutingAssembly().
GetManifestResourceStream("WpfApplication1.Dictionaries.pl_PL.aff");
// Create a dictionary object
// 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 spell checker culture
checker.Culture = culture;
}
private void button1_Click(object sender, RoutedEventArgs e) {
// Start checking the text in a text box
checker.Check(textEdit1);
}
User Interaction
When an user starts spelling check (calls the SpellChecker.Check method), the Spelling Dialog occurs.
You can use this dialog to make corrections, such as:
- 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
- Add the word to the custom dictionary (if the custom dictionary is available)
The Spelling dialog ships with a number of events that allow you to track user interaction.
The following events are available:
Event | Description |
---|---|
SpellingFormShowing | Occurs when the Spelling Dialog invokes. |
CheckCompleteFormShowing | Occurs when the spell checker finished checking and the message occurs. |
WordAdded | Fires after the word is added to a custom dictionary. |
AfterCheckWord | Fires after a word is checked. |
BeforeLoadDictionaries | Occurs before dictionaries are loaded. |
AfterLoadDictionaries | Occurs after the dictionaries are loaded. |
CustomDictionaryChanged | Occurs when the user modifies a custom dictionary. |