0

I would like to know if there's an easy way to implement spell check to my rich text box? I have heard there's a way to use the spell check from MS Word, but I would like to know if I could add an independent spell check to my application. If somebody could provide me with a tutorial on how to do this (video or webpage or example or anything), then I'd really appreciate it.

--EDIT--

Following on from the answer I received, I implemented the spell check to my code and it is now as follows:

private NetSpell.SpellChecker.Spelling spelling;
        private NetSpell.SpellChecker.Dictionary.WordDictionary wordDictionary;
        internal System.Windows.Forms.Button spellButton;
        internal System.Windows.Forms.RichTextBox demoRichText;
        private System.ComponentModel.IContainer components2;
        internal System.Windows.Forms.RichTextBox Document;
        internal NetSpell.SpellChecker.Spelling SpellChecker;
        private System.ComponentModel.IContainer components1;
        internal NetSpell.SpellChecker.Dictionary.WordDictionary WordDictionary;

...

private void toolStripButton1_Click(object sender, EventArgs e)
    {
        try
        {
            this.spelling.Text = this.richTextBoxPrintCtrl1.Text; // I get an error on this line.
            this.spelling.SpellCheck();
        }
        catch
        {
            MessageBox.Show("Error loading Spell Checker. Please reload application and try again.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void spelling_DeletedWord(object sender, NetSpell.SpellChecker.SpellingEventArgs e)
    {
        int start = this.richTextBoxPrintCtrl1.SelectionStart;
        int length = this.richTextBoxPrintCtrl1.SelectionLength;

        this.richTextBoxPrintCtrl1.Select(e.TextIndex, e.Word.Length);
        this.richTextBoxPrintCtrl1.SelectedText = "";

        if (start > this.richTextBoxPrintCtrl1.Text.Length)
            start = this.richTextBoxPrintCtrl1.Text.Length;

        if ((start + length) > this.richTextBoxPrintCtrl1.Text.Length)
            length = 0;

        this.richTextBoxPrintCtrl1.Select(start, length);
    }

    private void spelling_ReplacedWord(object sender, NetSpell.SpellChecker.ReplaceWordEventArgs e)
    {
        int start = this.richTextBoxPrintCtrl1.SelectionStart;
        int length = this.richTextBoxPrintCtrl1.SelectionLength;

        this.richTextBoxPrintCtrl1.Select(e.TextIndex, e.Word.Length);
        this.richTextBoxPrintCtrl1.SelectedText = e.ReplacementWord;

        if (start > this.richTextBoxPrintCtrl1.Text.Length)
            start = this.richTextBoxPrintCtrl1.Text.Length;

        if ((start + length) > this.richTextBoxPrintCtrl1.Text.Length)
            length = 0;

        this.richTextBoxPrintCtrl1.Select(start, length);
    }



     private void spelling_EndOfText(object sender, System.EventArgs e)
        {
            Console.WriteLine("EndOfText");
}

However, when I try to load the checker, I get a NullReferenceExeption was unhandled error on the line uncommented in the code.

Any ideas? I don't know where to go from this point. I can load the program, but I get an error on the uncommented line of code. I've tried following the demo, but I can't seem to see why the demos code works but mine refuses to play nice... My code is exactly the same as the demo example (from what I can see), so why is it now working and giving me an error when I try to run the spell checker?

Toby
  • 377
  • 1
  • 10
  • 23
  • I think you will find an answer to your question in this thread: http://stackoverflow.com/questions/453611/what-is-the-best-spell-checking-library-for-c – s_qw23 Apr 26 '13 at 22:20
  • @s_qw23 which part of this thread are you referring to specifically? :o) – Toby Apr 26 '13 at 22:21
  • at this one: "if I could add an independent spell check to my application". If its not matching what you are searching for I'm sry :D Thought you were searching a module or something but if you want to write it on your own its probably not the right source. – s_qw23 Apr 26 '13 at 22:24
  • @s_qw23 Do you have any idea how I could implement NHunspell to my application? I have no idea where to start.. :) – Toby Apr 26 '13 at 22:27
  • 1
    Start with [the documentation](http://www.maierhofer.de/en/documentation/nhunspell/index.html) perhaps. – Cody Gray - on strike Apr 26 '13 at 22:34

1 Answers1

3

It's a little older, but I have personally used NetSpell which seems to be pretty easy to set up, just include the project in your Visual Studio solution and it should be good to go.

http://www.codeproject.com/Articles/5277/NetSpell-Spell-Checker-for-NET

Joe Korolewicz
  • 474
  • 4
  • 21
  • Thanks for the link. Any tips on how to implement this? – Toby Apr 26 '13 at 23:15
  • Because the demo looks promising. – Toby Apr 26 '13 at 23:42
  • After you download it, you should be able to pull down the project in Visual Studio, once you compile the project, you can reference the DLL in your solution. Once you have the reference, about half way down the page I linked earlier, there is a "Using the Library" which pretty much covers the code to run the spell check. – Joe Korolewicz Apr 26 '13 at 23:45
  • I'm getting around thirteen errors when I implement the code! I have no clue what to do. Where abouts am I _supposed_ to put the code on my .cs file? – Toby Apr 27 '13 at 00:02
  • The project download should contain an example. You should be adding those events somewhere in your class, creating a SpellChecker object. And set the spellChecker.Text and run spellChecker.SpellCheck() whenever you need to run the spellcheck, perhaps on a click from the UI? – Joe Korolewicz Apr 27 '13 at 00:13
  • This is really confusing me. Sorry, I'm new to programming and just trying to learn... I did take a look at the demo project but I couldn't see the correct code.. – Toby Apr 27 '13 at 00:17
  • Okay.. I've got the program built, but when I try to run the SpellChecker, the program crashes and I get the error "NullReferenceExeption was unhandled" as the line "this.spelling.Text = this.richTextBoxPrintCtrl1.Text;" – Toby Apr 27 '13 at 00:32
  • Any ideas what could be causing this? – Toby Apr 27 '13 at 17:01
  • I would guess this.richTextBoxPrintCtrl1.Text is null. Have you seen what the value is when debugging? – Joe Korolewicz Apr 27 '13 at 19:14