【问题标题】:Spell Checker using word使用单词的拼写检查器
【发布时间】:2012-02-27 20:49:27
【问题描述】:

我正在为拼写检查器编写 C# 代码。我在网上找到了这段代码。我是 c# 新手,无法理解代码。

我在以下网站上找到了此代码: http://www.codeproject.com/Articles/4572/Using-Word-s-spellchecker-in-C

我可以了解一下代码中实际发生的事情的一般准则吗:

using Word;

using System.Reflection;


private void button1_Click(object sender, System.EventArgs e) 

{ 

    fSpellCheck(textBox1 , label1 ); 

}



public void fSpellCheck(TextBox tBox, Label lLbl) 

{ 

    int iErrorCount = 0; 

    Word.Application app = new Word.Application(); 

    if (tBox.Text.Length > 0) 

    { 

        app.Visible=false; 

        // Setting these variables is comparable

        // to passing null to the function. 

        // This is necessary because the C# null

        // cannot be passed by reference. 

        object template=Missing.Value; 

        object newTemplate=Missing.Value; 

        object documentType=Missing.Value; 

        object visible=true; 

        object optional = Missing.Value; 

        _Document doc = app.Documents.Add(ref template, 

           ref newTemplate, ref documentType, ref visible); 

        doc.Words.First.InsertBefore (tBox.Text ); 

        Word.ProofreadingErrors we = doc.SpellingErrors; 

        iErrorCount = we.Count; 

        doc.CheckSpelling( ref optional, ref optional, ref optional, 

            ref optional, ref optional, ref optional, ref optional, 

            ref optional, ref optional, ref optional, 

            ref optional, ref optional); 

        if (iErrorCount == 0) 

            lLbl.Text = "Spelling OK. No errors corrected "; 

        else if (iErrorCount == 1) 

            lLbl.Text = "Spelling OK. 1 error corrected "; 

        else 

            lLbl.Text = "Spelling OK. " + iErrorCount + 

                                    " errors corrected "; 

        object first=0; 

        object last=doc.Characters.Count -1; 

        tBox.Text = doc.Range(ref first, ref last).Text; 

    } 

    else 

        lLbl.Text = "Textbox is empty"; 
    object saveChanges = false; 

    object originalFormat = Missing.Value; 

    object routeDocument = Missing.Value; 

    app.Quit(ref saveChanges, ref originalFormat, ref routeDocument); 

}

【问题讨论】:

  • 我们回答了您的问题吗?请回复。

标签: c# office-interop


【解决方案1】:

我在 C# 方面没有太多经验,但我想你有一个带有 GUI、按钮、标签和文本框的应用程序。

需要button1_Click 方法为按钮分配一个动作,当被点击时,按钮的一种事件监听器。 当你点击这个按钮时,button1_Click被执行,fSpellCheck(textBox1 , label1 );方法被调用。

方法fSpellCheck(textBox1 , label1 ); 实现了检查单词的算法,由文本框插入(注意fSpellCheck 参数中的文本框引用)。该方法检查单词是否有错误,如果单词正确或错误(有错误)或文本框为空,则该方法将在标签lLbl 中打印单词控制的结果。

【讨论】:

    【解决方案2】:

    这基本上就是上面的代码所做的。

    1) 它以隐藏模式打开一个新的 Word 实例
    2) 它将文本框中的文本插入文档的第一部分
    3) 它在 word 文档上调用拼写检查器
    4) 它从拼写检查器获取错误计数并将错误数量打印到您的标签
    5) 它要求 Word 更正文档中的错误。
    6) 它将更正后的文本从 word 文档复制回您的文本框
    7) 它关闭文档并退出隐藏的 Word 实例。

    您需要在项目中做什么:
    1. 创建一个标签(如果您还没有)
    2. 创建一个文本框(如果您还没有)
    3. 创建一个按钮(如果您还没有)

    为您的按钮添加一个点击事件,然后在该代码中调用 fSpellCheck,就像在此代码中所做的那样,使用您的标签和您的文本框作为参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      相关资源
      最近更新 更多