Edit this page

Spellcheck

The RadRichTextBox control is designed to support "spell checking as you type" by setting a single property and specifying a proper dictionary to it. This topic explains you the following:

Enabling SpellCheck

To enable or disable the spell-checking functionality, you can use the IsSpellCheckingEnabled property on the RadRichTextBox. When the property is enabled, you see red wavy underlines below unrecognized words. When the property is False, no dictionaries are loaded and no overhead is incurred for spell checking.

As the default value is True, the only thing you need to do in order to get a fully functioning spell checker is to add a reference in your project to the Telerik.Windows.Documents.Proofing.Dictionaries.En-US.dll assembly. It contains a built-in English dictionary, which gets automatically added to the RadRichTextBox thanks to the usage of MEF.

You can customize the spell checker by using the SpellChecker property of RadRichTextBox. It is of type ISpellChecker. By default an object of type DocumentSpellChecker, which implements the interface, is used for this property. You can either use it or provide your custom class that implements the ISpellChecker interface.

When spell checking is enabled using IsSpellCheckingEnabled property, spell checker is asynchronously invoked in a background thread and marks the words in the document as valid or invalid. If you need to invalidate the words currently marked, you can use RadRichTextBox.InvalidateProofingErrors(bool) method.

Dictionaries

The dictionaries in RadRichTextBox implement the IWordDictionary interface. Easy interoperability with dictionaries from RadSpell for ASP.NET is achieved through the RadDictionary class, which supports the loading of a dictionary directly from the *.tdf files, used with RadSpell. You can find TDF dictionaries for some languages here.

Here is an example of a RadDictionary loaded from a TDF file.

When adding a RadDictionary or similar object, use the AddDictionary(IWordDictionary dictionary, CultureInfo culture) method of the DocumentSpellChecker. You can also associate a dictionary with a specific culture. The method to remove this dictionary is RemoveDictionary(CultureInfo culture).

[C#] Example 1: Load a dictionary

private void LoadDictionary(Stream tdfFileStream)
{
    RadDictionary dictionary = new RadDictionary();
    dictionary.Load(tdfFileStream);
    ((DocumentSpellChecker)this.radRichTextBox.SpellChecker).AddDictionary(dictionary, CultureInfo.InvariantCulture);
}

[VB.NET] Example 1: Load a dictionary

Private Sub LoadDictionary(ByVal tdfFileStream As Stream)
 Dim dictionary As New RadDictionary()
 dictionary.Load(tdfFileStream)
 CType(Me.radRichTextBox.SpellChecker, DocumentSpellChecker).AddDictionary(dictionary, CultureInfo.InvariantCulture)
End Sub

The given example doesn't contain the logic used to read the TDF file as a Stream.

RadWordsDictionary

RadWordsDictionary implements the IWordDictionary interface and is capable of reading words from text files that contain one word per line. An example of such files are the .dic dictionaries. Example 2 shows you how to create a RadWordsDictionary instance from a stream.

[C#] Example 2: Load stream in RadWordsDictionary

RadWordsDictionary dictionary = new RadWordsDictionary();
dictionary.Load(stream);

[VB.NET] Example 2: Load stream in RadWordsDictionary

Dim dictionary As New RadWordsDictionary()
dictionary.Load(stream)

One of the constructor overloads of the RadWordsDictionary class enables you to pass a parameter of type IEnumerable<string>, which can help you create your own dictionary with a custom set of words.

[C#] Example 3: Create RadWordsDictionary from strings

List<string> words = new List<string>();
words.Add("Test");
words.Add("Teacher");
words.Add("Sister");

RadWordsDictionary dictionary = new RadWordsDictionary();
dictionary.Load(words);

[VB.NET] Example 3: Create RadWordsDictionary from strings

Dim words As New List(Of String)()
words.Add("Test")
words.Add("Teacher")
words.Add("Sister")

Dim dictionary As New RadWordsDictionary()
dictionary.Load(words)

Custom Dictionaries

One feature of the SpellChecker in RadRichTextBox is the ability to add a word to a user-defined custom dictionary to enable domain-specific applications. Such a dictionary should implement the IWordCustomDictionary interface. For that, the RadRichTextBox provides you with the RadIsolatedStorageCustomDictionary class out of the box. It allows you to persist the word list in a file located in the WPF’s Isolated Storage.

Here is an example of such a dictionary that adds the words in the "CustomDictionary.txt" marked as a Site-scoped IsolatedSotrageFile.

The IsolatedStorageScope enumeration has the following values:

  • Site: The dictionary defined in this scope is available for each application hosted on the same site.
  • Application: The dictionary is available only for the particular application.

When adding a dictionary that implements the ICustomWordDictionary interface object use the AddCustomDictionary(ICustomWordDictionary customDictionary, CultureInfo culture) method of the DocumentSpellChecker class. You can also associate a custom dictionary to a specific culture. The method to remove it is RemoveCustomDictionary(ICustomWordDictionary customDictionary, CultureInfo culture).

[C#] Example 4: Creating a custom dictionary

private void CreateCustomDictionary()
{
    RadIsolatedStorageCustomDictionary dictionary = new RadIsolatedStorageCustomDictionary(IsolatedStorageScope.Site, "CustomDictionary.txt");
    DocumentSpellChecker spellchecker = new DocumentSpellChecker(dictionary);
    this.radRichTextBox.SpellChecker = spellchecker;
}

[VB.NET] Example 4: Creating a custom dictionary

Private Sub CreateCustomDictionary()
 Dim dictionary As New RadIsolatedStorageCustomDictionary(IsolatedStorageScope.Site, "CustomDictionary.txt")
 Dim spellchecker As New DocumentSpellChecker(dictionary)
 Me.radRichTextBox.SpellChecker = spellchecker
End Sub

If you want to have a temporary custom dictionary, which will only be available for a single application session, you can use the RadNonPersistentCustomDictionary object.

Adding a Word

To add a word to a dictionary you can either use the AddWord() method of the DocumentSpellChecker or of the dictionary itself. Using the AddWord() method, you can add a word to multiple dictionaries associated to the same culture. You can do this by passing the desired culture as a parameter to the method.

Using the overload of the AddWord() method, which takes only the word as an argument, is equal to using the second overload and passing CultureInfo.InvariantCulture as an argument.

Using the AddWord() method of the dictionary itself adds the word only to the respective dictionary.

[C#] Example 5: Adding a word to a dictionary

this.radRichTextBox.SpellChecker.AddWord("RadRichTextBox", CultureInfo.InvariantCulture);

[VB.NET] Example 5: Adding a word to a dictionary

Me.radRichTextBox.SpellChecker.AddWord("RadRichTextBox", CultureInfo.InvariantCulture)

Internationalization

The spell-checking component is designed to suit scenarios where different cultures take place in the same application. Internationalization is achieved through associating each dictionary and custom dictionary with a specific culture (or the InvariantCulture as the default one).

Here is an example.

The given example doesn't contain the logic used to read the TDF file as a Stream.

[C#] Example 6: Loading a dictionary and associating it with a culture

private void LoadDictionaryWithCulture(Stream tdfFileStream)
{
    RadDictionary dictionary = new RadDictionary();
    dictionary.Load(tdfFileStream);
    ((DocumentSpellChecker)this.radRichTextBox.SpellChecker).AddDictionary(dictionary, new CultureInfo("de-DE"));
}

[VB.NET] Example 6: Loading a dictionary and associating it with a culture

 Private Sub LoadDictionaryWithCulture(ByVal tdfFileStream As Stream)
 Dim dictionary As New RadDictionary()
 dictionary.Load(tdfFileStream)
 CType(Me.radRichTextBox.SpellChecker, DocumentSpellChecker).AddDictionary(dictionary, New CultureInfo("de-DE")