wpf-17827-controls-and-libraries-spell-checker-getting-started-manage-the-spell-checker-in-code-behind.md
This tutorial describes how to start a spell check in a DevExpress TextEdit control on a Button click.
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.25.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 references to the following libraries:
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 25.2\Components\Bin\Framework_
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.
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);
}
Imports System.IO
Imports System.Windows
Imports System.Reflection
Imports System.Globalization
Imports DevExpress.Xpf.SpellChecker
Imports DevExpress.XtraSpellChecker
Private checker As SpellChecker
public MainWindow()
Public Sub New()
checker = New SpellChecker()
InitializeComponent()
' Obtain dictionary information from the assembly
Dim dict As Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("WpfApplication1.Dictionaries.pl_PL.dic")
Dim grammar As Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("WpfApplication1.Dictionaries.pl_PL.aff")
' Create a dictionary object
' and load information into it
Dim dictionary As New SpellCheckerOpenOfficeDictionary()
dictionary.LoadFromStream(dict, grammar, Nothing)
' Define the dictionary culture
Dim culture As 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
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
' Start checking the text in a text box
checker.Check(textEdit1)
End Sub
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:
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. |