C#拼写检查问题

C#拼写检查问题,c#,winforms,c#-3.0,ms-word,spell-checking,C#,Winforms,C# 3.0,Ms Word,Spell Checking,我已将拼写检查纳入我的win forms C#项目。这是我的密码 public void CheckSpelling() { try { // declare local variables to track error count // and information int SpellingErrors = 0; string ErrorCountMessage = string.Empty;

我已将拼写检查纳入我的win forms C#项目。这是我的密码

public void CheckSpelling()
{
    try
    {
        // declare local variables to track error count 
        // and information
        int SpellingErrors = 0;
        string ErrorCountMessage = string.Empty;

        // create an instance of a word application
        Microsoft.Office.Interop.Word.Application WordApp =
            new Microsoft.Office.Interop.Word.Application();

        // hide the MS Word document during the spellcheck
        //WordApp.WindowState = WdWindowState.wdWindowStateMinimize;


        // check for zero length content in text area
        if (this.Text.Length > 0)
        {
            WordApp.Visible = false;

            // create an instance of a word document
            _Document WordDoc = WordApp.Documents.Add(ref emptyItem,
                                              ref emptyItem,
                                              ref emptyItem,
                                              ref oFalse);

            // load the content written into the word doc
            WordDoc.Words.First.InsertBefore(this.Text);

            // collect errors form new temporary document set to contain
            // the content of this control
            Microsoft.Office.Interop.Word.ProofreadingErrors docErrors = WordDoc.SpellingErrors;
            SpellingErrors = docErrors.Count;

            // execute spell check; assumes no custom dictionaries
            WordDoc.CheckSpelling(ref oNothing, ref oIgnoreUpperCase, ref oAlwaysSuggest,
                ref oNothing, ref oNothing, ref oNothing, ref oNothing, ref oNothing,
                ref oNothing, ref oNothing, ref oNothing, ref oNothing);

            // format a string to contain a report of the errors detected
            ErrorCountMessage = "Spell check complete; errors detected: " + SpellingErrors;

            // return corrected text to control's text area
            object first = 0;
            object last = WordDoc.Characters.Count - 1;
            this.Text = WordDoc.Range(ref first, ref last).Text;
        }
        else
        {
            // if nothing was typed into the control, abort and inform user
            ErrorCountMessage = "Unable to spell check an empty text box.";
        }

        WordApp.Quit(ref oFalse, ref emptyItem, ref emptyItem);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp);

        // return report on errors corrected
        // - could either display from the control or change this to 
        // - return a string which the caller could use as desired.
       // MessageBox.Show(ErrorCountMessage, "Finished Spelling Check");
    }
    catch (Exception e)
    {
        MessageBox.Show(e.ToString());
    }
}

拼写检查器工作得很好,唯一的问题是当我试图移动拼写检查器时,由于某种原因,主窗体模糊了。当我关闭拼写检查器时,主窗体也会恢复正常。它似乎正在打开Microsoft word,然后隐藏窗口,只允许看到拼写检查程序。请提供帮助。

我尝试使用您的示例代码,但效果不理想,所以我尝试了

也就是说,我发现这是一个相当棘手的解决方案。至于你的主窗体模糊了,我猜是因为当你在拼写检查窗口时它停止了响应?您可以通过使用新线程绕过它

另外,你是对的,它正在启动MS Word,然后隐藏窗口


就个人而言,我宁愿使用类似于Office的库,而不是依赖于Office。

我尝试使用您的示例代码,但效果不理想,所以我尝试了

也就是说,我发现这是一个相当棘手的解决方案。至于你的主窗体模糊了,我猜是因为当你在拼写检查窗口时它停止了响应?您可以通过使用新线程绕过它

另外,你是对的,它正在启动MS Word,然后隐藏窗口


就我个人而言,我宁愿使用像Office这样的库,而不是依赖Office。

我的工作和测试代码片段如下:

string s1 = textBox1.Text;

Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();

Microsoft.Office.Interop.Word._Document doc = app.Documents.Add();

doc.Words.First.InsertBefore(s1);

Microsoft.Office.Interop.Word.ProofreadingErrors errors = doc.SpellingErrors;

int errorCount = errors.Count;

doc.CheckSpelling(Missing.Value, true, false);

app.Quit(false);

textBox3.Text = errorCount.ToString();
应用程序的文本错误

窗口将错误的单词显示为红色突出显示的文本

最后显示的是总错误计数


解决方案取自我的。

我的工作和测试代码片段如下:

string s1 = textBox1.Text;

Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();

Microsoft.Office.Interop.Word._Document doc = app.Documents.Add();

doc.Words.First.InsertBefore(s1);

Microsoft.Office.Interop.Word.ProofreadingErrors errors = doc.SpellingErrors;

int errorCount = errors.Count;

doc.CheckSpelling(Missing.Value, true, false);

app.Quit(false);

textBox3.Text = errorCount.ToString();
应用程序的文本错误

窗口将错误的单词显示为红色突出显示的文本

最后显示的是总错误计数


解决方案来自我的。

我还正在寻找WinForms拼写检查器。我很好奇:你还在用NetSpell吗?首先,你答案中的链接指向一篇将近10年前的文章(2003年)。此外,该图像仅显示一个对话框。这是否意味着它不能在RichTextBox控件中显示那些红色的曲线?谢谢。我也在找WinForms拼写检查器。我很好奇:你还在用NetSpell吗?首先,你答案中的链接指向一篇将近10年前的文章(2003年)。此外,该图像仅显示一个对话框。这是否意味着它不能在RichTextBox控件中显示那些红色的曲线?谢谢