Skip to main content

How to: Save words added via the Add to Dictionary option to a custom physical dictionary

  • 3 minutes to read

Currently, ASPxRichEdit doesn’t save words added by a user via the “Add to Dictionary” option in the physical dictionary file and stores these changes in a Session variable.If it’s necessary to save changes from the temporary dictionary stored in Session to a physical dictionary file on demand, you can accomplish this task programmatically, for example, on a button click. Get the temporary dictionary that has the SpellCheckerCachedCustomDictionary type from Session in the following way:

SpellCheckerCachedCustomDictionary dic = Session[ASPxRichEdit1.Settings.SpellChecker.Dictionaries[0].CacheKey] as SpellCheckerCachedCustomDictionary;

Note that you need to pass an index of your custom dictionary to the collection of other dictionaries used in your project as the Dictionaries collection index in this code.Then, pass words from that temporary dictionary to a new instance of your custom dictionary class derived from SpellCheckerCustomDictionary and save this custom dictionary to the appropriate physical path obtained from the temporary dictionary’s DictionaryPath property:

MyCustomDictionary dictionary = new MyCustomDictionary();
for (int i = 0; i < dic.WordCount; i++)
    dictionary.AddWord(dic[i]);
dictionary.SaveAs(dic.DictionaryPath);
using DevExpress.XtraSpellChecker;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e) {

    }
    protected void ASPxButton1_Click(object sender, EventArgs e) {
        SpellCheckerCachedCustomDictionary dic = Session[ASPxRichEdit1.Settings.SpellChecker.Dictionaries[0].CacheKey] as SpellCheckerCachedCustomDictionary;
        MyCustomDictionary dictionary = new MyCustomDictionary();
        for (int i = 0; i < dic.WordCount; i++)
            dictionary.AddWord(dic[i]);
        dictionary.SaveAs(dic.DictionaryPath);
    }
}

public class MyCustomDictionary : SpellCheckerCustomDictionary
{
    public MyCustomDictionary() : base() { }
    public MyCustomDictionary(string dictionaryPath, CultureInfo culture) : base(dictionaryPath, culture) { }
    public override bool Loaded { get { return true; } }
}