Back to Devexpress

How to: Load a User-Specific Dictionary and Update It During Spell Check

aspnet-9506-components-spell-checker-examples-how-to-load-a-user-specific-dictionary-and-update-it-during-spell-check.md

latest5.5 KB
Original Source

How to: Load a User-Specific Dictionary and Update It During Spell Check

  • Dec 17, 2020
  • 3 minutes to read

A custom dictionary for ASPxSpellChecker can be specified in the page markup or created in the code-behind file. This dictionary is the same for different users. Words added to a custom dictionary during a spell check are stored in a session cache, so the original custom dictionary remains intact.

This article demonstrates how to load a dictionary that is specific to a particular user. The user has the ability to extend this dictionary. To accomplish this task, you should handle two ASPxSpellChecker events - the ASPxSpellChecker.CustomDictionaryLoading event and the ASPxSpellChecker.WordAdded event.

CustomDictionaryLoading Event

The following code snippet illustrates that you can handle the ASPxSpellChecker.CustomDictionaryLoading event to load a custom dictionary from a stream. Note that the ASPxSpellChecker control already has an ASPxSpellCheckerCustomDictionary object in its collection of dictionaries. When the custom dictionary is loading, it fires an event allowing you to load a user-specific dictionary.

View Example

csharp
using DevExpress.Web.ASPxSpellChecker;
using DevExpress.XtraSpellChecker;
        protected void ASPxSpellChecker1_CustomDictionaryLoading(object sender, CustomDictionaryLoadingEventArgs e) {

            FileStream alphStream = new FileStream(Server.MapPath(e.CustomDictionary.AlphabetPath), FileMode.Open, FileAccess.Read);
            FileStream dicStream = new FileStream(userDictPath, FileMode.OpenOrCreate, FileAccess.Read);
            try {
                alphStream.Seek(0, SeekOrigin.Begin);
                dicStream.Seek(0, SeekOrigin.Begin);
                e.CustomDictionary.LoadFromStream(dicStream, alphStream);
            }
            finally {
                alphStream.Dispose();
                dicStream.Dispose();
            }
        }
vb
Imports DevExpress.Web.ASPxSpellChecker
Imports DevExpress.XtraSpellChecker
        Protected Sub ASPxSpellChecker1_CustomDictionaryLoading(ByVal sender As Object, ByVal e As CustomDictionaryLoadingEventArgs)

            Dim alphStream As New FileStream(Server.MapPath(e.CustomDictionary.AlphabetPath), FileMode.Open, FileAccess.Read)
            Dim dicStream As New FileStream(userDictPath, FileMode.OpenOrCreate, FileAccess.Read)
            Try
                alphStream.Seek(0, SeekOrigin.Begin)
                dicStream.Seek(0, SeekOrigin.Begin)
                e.CustomDictionary.LoadFromStream(dicStream, alphStream)
            Finally
                alphStream.Dispose()
                dicStream.Dispose()
            End Try
        End Sub

WordAdded Event

This code illustrates that you can handle the ASPxSpellChecker.WordAdded event to save an updated custom dictionary to the original location. The ASPxSpellChecker.GetCustomDictionary method enables you to access a custom dictionary that is currently in use.

View Example

csharp
using DevExpress.Web.ASPxSpellChecker;
using DevExpress.XtraSpellChecker;
protected void ASPxSpellChecker1_WordAdded(object sender, WordAddedEventArgs e) {
    ASPxSpellChecker checker = send as ASPxSpellChecker;
    if (checker != null) {
        SpellCheckerCustomDictionary dic = checker.GetCustomDictionary();
        if (dic != null) {
            StringBuilder stb = new StringBuilder();
            for (int i = 0; i < dic.WordCount; i++) stb.AppendLine(dic[i]);
            using (StreamWriter writer = new StreamWriter(dic.DictionaryPath,false, Encoding.UTF8)) {
                writer.Write(stb.ToString());
            }
        }
    }
}
vb
Imports DevExpress.Web.ASPxSpellChecker
Imports DevExpress.XtraSpellChecker
Protected Sub ASPxSpellChecker1_WordAdded(ByVal sender As Object, ByVal e As WordAddedEventArgs)

    Dim checker As ASPxSpellChecker = TryCast(send, ASPxSpellChecker)
    If checker IsNot Nothing Then
        Dim dic As SpellCheckerCustomDictionary = checker.GetCustomDictionary()
        If dic IsNot Nothing Then
            Dim stb As StringBuilder = New StringBuilder()
            For i As Integer = 0 To dic.WordCount - 1
                stb.AppendLine(dic(i))
            Next
            Using writer As StreamWriter = New StreamWriter(dic.DictionaryPath, False, Encoding.UTF8)
                writer.Write(stb.ToString())
            End Using
        End If
    End If
End Sub

See Also

ASPxSpellCheckerCustomDictionary

GetCustomDictionary()