如何在我的富文本框中添加拼写检查?

时间:2013-04-26 22:17:56

标签: c# visual-studio .net-4.0 richtextbox

我想知道是否有一种简单的方法可以对我的富文本框执行拼写检查?我听说有一种方法可以使用MS Word的拼写检查,但我想知道是否可以在我的应用程序中添加一个独立的拼写检查。如果有人可以提供有关如何执行此操作的教程(视频或网页或示例或任何内容),那么我真的 欣赏它。

- 编辑 -

根据我收到的答案,我对我的代码实施了拼写检查,现在如下:

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");
}

然而,当我尝试加载检查器时,我在代码中未注释的行上得到NullReferenceExeption未处理的错误。

有什么想法吗?我不知道从哪里开始。我可以加载程序,但是在未注释的代码行上出现错误。我已经尝试过跟随演示了,但我似乎无法理解为什么演示代码可以正常工作但是我拒绝播放...我的代码与演示示例完全相同(从我所看到的),所以为什么当我尝试运行拼写检查时它现在正在工作并给我一个错误吗?

1 个答案:

答案 0 :(得分:3)

它有点老了,但我个人使用的NetSpell似乎很容易设置,只需在Visual Studio解决方案中包含该项目,它应该是好的。

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