How to: Load a User-Specific Dictionary and Update It During Spell Check
- 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.
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();
}
}
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.
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());
}
}
}
}