ComponentOne RichTextBox for WPF
Working with WPF RichTextBox / Spell-Checking
In This Topic
    Spell-Checking
    In This Topic

    Most rich editors implement two types of spell-checking:

    RichTextBox supports both types of spell-checking using the C1SpellChecker component, included in WPF Edition. Please note that the C1SpellChecker ships as a separate assembly altogether because it can be used with various other controls for spell-checking.

    Modal Spell Checking

    To implement modal spell checking, you need to add a reference to the C1.WPF.SpellChecker assembly to your project. Then, add the following code to your project. This code creates a new C1SpellChecker object to be shared by all controls on the page that require spell-checking. Later, the page constructor invokes the Load method to load the main spelling dictionary from a stream containing the compressed Dictionary data. C1SpellChecker includes over 20 other dictionaries which can be downloaded from our site. In this case, we are loading C1Spell_en-US.dct, the American English dictionary. This file must be present on the application folder. When the modal checking is complete, the _c1SpellChecker_CheckControlCompleted event fires and shows a dialog box to indicate that the spell-checking operation is complete.

    spellchecker used with richtextbox

    C#
    Copy Code
    public partial class SpellCheckerRichTextBoxDemo : UserControl
        {
            // declare the C1SpellChecker
            C1SpellChecker _c1SpellChecker = new C1SpellChecker();
    
            public SpellCheckerRichTextBoxDemo()
            {
                InitializeComponent();
                this.Tag = Properties.Resources.SpellCheckerRtbDemoDescription;
                Loaded += Page_Loaded;
                Unloaded += Page_Unloaded;
            }
    
            void Page_Loaded(object sender, RoutedEventArgs e)
            {
    
                // connect toolbar to C1RichTextBox
                _rtbToolbar.RichTextBox = _richTextBox;
                _richTextBox.SpellChecker = _c1SpellChecker;
    
                // load sample text into text boxes
                using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("SpellCheckerExplorer.Resources.test.txt"))
                using (var sr = new StreamReader(stream))
                {
                    var text = sr.ReadToEnd();
                    _richTextBox.Text = text;
                }
    
                // set up ignore list
                WordList il = _c1SpellChecker.IgnoreList;
                il.Add("ComponentOne");
                il.Add("Silverlight");
    
                // monitor events
                _c1SpellChecker.BadWordFound += _c1SpellChecker_BadWordFound;
                _c1SpellChecker.CheckControlCompleted += _c1SpellChecker_CheckControlCompleted;
    
                // load main dictionary
                if (_c1SpellChecker.MainDictionary.State != DictionaryState.Loaded)
                    _c1SpellChecker.MainDictionary.Load(Application.GetResourceStream(new Uri("/" + new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + ";component/Resources/C1Spell_en-US.dct", UriKind.Relative)).Stream);
                if (_c1SpellChecker.MainDictionary.State == DictionaryState.Loaded)
                {
                    WriteLine("loaded main dictionary ({0:n0} words).", _c1SpellChecker.MainDictionary.WordCount);
                }
                else
                {
                    WriteLine("failed to load dictionary: {0}", _c1SpellChecker.MainDictionary.State);                    
                }
                
                // save user dictionary when app exits
                App.Current.Exit += App_Exit;
            }
    
            void Page_Unloaded(object sender, RoutedEventArgs e)
            {
                _c1SpellChecker.BadWordFound -= _c1SpellChecker_BadWordFound;
                _c1SpellChecker.CheckControlCompleted -= _c1SpellChecker_CheckControlCompleted;
                
            }
    
            // monitor spell-checker events
            void _c1SpellChecker_CheckControlCompleted(object sender, CheckControlCompletedEventArgs e)
            {
                if (!e.Cancelled)
                {
                    var msg = string.Format("Spell-check complete, {0} errors found.", e.ErrorCount);
                    MessageBox.Show(msg, "Spelling");
                }
                WriteLine("CheckControlCompleted: {0} errors found", e.ErrorCount);
                if (e.Cancelled)
                {
                    WriteLine("\t(cancelled...)");
                }
            }
            void _c1SpellChecker_BadWordFound(object sender, BadWordEventArgs e)
            {
                WriteLine("BadWordFound: \"{0}\" {1}", e.BadWord.Text, e.BadWord.Duplicate ? "(duplicate)" : string.Empty);
            }              
        }