【问题标题】:How can I add spell-checking to my rich text box?如何在富文本框中添加拼写检查?
【发布时间】:2013-04-21 04:42:04
【问题描述】:

我想知道是否有一种简单的方法可以对我的富文本框实施拼写检查?我听说有一种方法可以使用 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 未处理错误。

有什么想法吗?我不知道从这一点开始。我可以加载程序,但在未注释的代码行上出现错误。我试过关注演示,但我似乎看不出为什么演示代码有效,但我的代码拒绝发挥良好......我的代码与演示示例完全相同(据我所见),所以为什么当我尝试运行拼写检查器时,它现在是否正常工作并给我一个错误?

【问题讨论】:

  • 我想你会在这个帖子中找到你的问题的答案:stackoverflow.com/questions/453611/…
  • @s_qw23 您具体指的是该线程的哪一部分? :o)
  • 在这个:“如果我可以在我的应用程序中添加一个独立的拼写检查”。如果它与您正在搜索的内容不匹配,我很抱歉:D 以为您正在搜索模块或其他东西,但如果您想自己编写它可能不是正确的来源。
  • @s_qw23 你知道如何在我的应用程序中实现 NHunspell 吗?我不知道从哪里开始.. :)
  • 或许以the documentation开头。

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


【解决方案1】:

它有点老了,但我个人使用过 NetSpell,它似乎很容易设置,只需将项目包含在您的 Visual Studio 解决方案中,就可以了。

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

【讨论】:

  • 感谢您的链接。关于如何实现这一点的任何提示?
  • 因为演示看起来很有希望。
  • 下载后,您应该可以在Visual Studio中拉下项目,一旦您编译项目,您就可以在您的解决方案中引用DLL。获得参考后,在我之前链接的页面的大约一半处,有一个“使用库”,它几乎涵盖了运行拼写检查的代码。
  • 我在实现代码时遇到了 13 个错误!我不知道该怎么做。我应该在哪里将代码放在我的 .cs 文件中?
  • 项目下载应该包含一个例子。您应该在类中的某处添加这些事件,创建一个 SpellChecker 对象。并设置 spellChecker.Text 并在您需要运行拼写检查时运行 spellChecker.SpellCheck() ,也许在 UI 中单击?
猜你喜欢
  • 2020-10-05
  • 1970-01-01
  • 2016-08-02
  • 1970-01-01
  • 2010-12-04
  • 1970-01-01
  • 2016-03-14
  • 2011-02-02
相关资源
最近更新 更多