首页 文章

将SpellCheck与Windows窗体一起使用...是否有可能将数据格式视图文本单元格装入?

提问于
浏览
1

我已成功使用Hans Passant(Trying to use the C# SpellCheck class)描述的方法将WPF SpellCheck类合并到我的Windows窗体应用程序中,该文本框用于将数据输入到Datagridview中 .

但是我正在努力弄清楚如何装配东西,以便当用户去编辑Datagridview中已经提交的条目时,该文本单元格会以某种方式转换为也可以使用SpellCheck的文本框 .

并且,我想,即使该列中的单元格未被编辑,最好的做法是将拼写错误的单词用红色加下划线 .

有没有办法做到这一点,而不必放弃使用Windows Forms for this app? (这是一个巨大的旧应用程序 . )

不确定我是否可以通过链接在我们可以通过Hans课程创建的拼写检查文本框中来覆盖DataGridView的编辑控件?

1 回答

  • 0
    private void button1_Click(object sender, EventArgs e)
        {
            Word.Application app = new Word.Application();
            int errors = 0;
            if (tx_article_title.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 visible = false;
                Word._Document doc1 = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);
                doc1.Words.First.InsertBefore(tx_article_title.Text);
                Word.ProofreadingErrors spellErrorsColl = doc1.SpellingErrors;
                errors = spellErrorsColl.Count;
                object optional = Missing.Value;
                doc1.Activate();
                this.Opacity = 0;
                //// get test form's handler
                //IntPtr hwnd = this.Handle;
                //// wait until the test form is the foreground window
                //while (true)
                //{
                //    if (GetForegroundWindow() == hwnd)
                //        break;
                //}
                //// Thread.Sleep(2000);
    
                // create a new thread to get the spelling check dialog                
                //Thread t = new Thread(new ThreadStart(GetSpellcheckingHandle));
                //t.Start();
                doc1.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);
                this.Opacity = 1;
                object first = 0;
                object last = doc1.Characters.Count - 1;
                tx_article_title.Text = doc1.Range(ref first, ref last).Text;
            }
            if (tx_author.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 visible = false;
                Word._Document doc1 = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);
                doc1.Words.First.InsertBefore(tx_author.Text);
                Word.ProofreadingErrors spellErrorsColl = doc1.SpellingErrors;
                errors = spellErrorsColl.Count;
                object optional = Missing.Value;
                doc1.Activate();
                this.Opacity = 0;
                //// get test form's handler
                //IntPtr hwnd = this.Handle;
                //// wait until the test form is the foreground window
                //while (true)
                //{
                //    if (GetForegroundWindow() == hwnd)
                //        break;
                //}
                //// Thread.Sleep(2000);
    
                // create a new thread to get the spelling check dialog                
                //Thread t = new Thread(new ThreadStart(GetSpellcheckingHandle));
                //t.Start();
                doc1.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);
                this.Opacity = 1;
                object first = 0;
                object last = doc1.Characters.Count - 1;
                tx_author.Text = doc1.Range(ref first, ref last).Text;
            }
            if (tx_region.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 visible = false;
    
                Word._Document doc1 = app.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);
                doc1.Words.First.InsertBefore(tx_region.Text);
                Word.ProofreadingErrors spellErrorsColl = doc1.SpellingErrors;
                errors = spellErrorsColl.Count;
                object optional = Missing.Value;
                doc1.Activate();
                this.Opacity = 0;
                //// get test form's handler
                //IntPtr hwnd = this.Handle;
                //// wait until the test form is the foreground window
                //while (true)
                //{
                //    if (GetForegroundWindow() == hwnd)
                //        break;
                //}
                //// Thread.Sleep(2000);
    
                // create a new thread to get the spelling check dialog                
                Thread t = new Thread(new ThreadStart(GetSpellcheckingHandle));
                t.Start();
                doc1.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);
                this.Opacity = 1;
                object first = 0;
                object last = doc1.Characters.Count - 1;
                tx_region.Text = doc1.Range(ref first, ref last).Text;
            }         
            object saveChanges = false;
            object originalFormat = Missing.Value;
            object routeDocument = Missing.Value;
            app.Quit(ref saveChanges, ref originalFormat, ref routeDocument);
            MessageBox.Show("SpellCheck completed!");
            this.Activate();
        }
    
        /// <summary>
        /// get the spelling check handle
        /// </summary>
        public void GetSpellcheckingHandle()
        {
            int i = FindWindow("bosa_sdm_msword", (int)IntPtr.Zero);
            while (i != 0)
            {
                // bring the spelling check dialog to the front.
                BringToFront("bosa_sdm_msword", (int)IntPtr.Zero);
            }
    
        }
    }
    

    这可能会有所帮助 .

相关问题