C#拼写检查问题 [英] C# Spell checker Problem

查看:196
本文介绍了C#拼写检查问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经合并拼写检查到我的胜利形成C#项目。这是我的代码。

I've incorporated spell check into my win forms C# project. This is my code.

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,然后隐藏窗口,只允许被看作拼写检查。请帮助。

The spell checker works well, the only problem is when I try to move the spell checker the main form blurs up for some reason. Also when I close the spell checker the main form is back to normal. It seems like it is opening up Microsoft word then hiding the window, only allowing the spell checker to be seen. Please help.

推荐答案

我试着用你的示例代码和它没有工作,以及它应该,所以我尝试 MSDN对这个问题的教程。

I tried using your sample code and it didn't work out as well as it should, so I tried MSDN's tutorial on the subject.

这是说,我觉得这是一个相当哈克的解决方案。至于你的主要形式模糊了起来,我想这是因为当你在拼写检查窗口停止响应?您可能能够通过使用一个新的线程来绕过它。

That said, I find it a rather hacky solution. As regards your main form blurring up, I guess it's because it stops responding while you are in the spellchecking window? You might be able to get around it by using a new thread.

另外,你说得对,它正在推出MS Word,然后隐藏窗口。

Also, you're right, it is launching MS Word, and then hiding the window.

就个人而言,我宁愿使用类似的 NetSpell 而不是依赖于办公室。

Personally, I'd rather use a library like NetSpell instead of relying on Office.

这篇关于C#拼写检查问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆