1

I have successfully used the method described by Hans Passant( Trying to use the C# SpellCheck class) to incorporate the WPF SpellCheck class into my Windows Forms app, on a text box that is used to enter data into a Datagridview.

But I'm struggling trying to figure out how to rig things so that when the user goes to EDIT an already-submitted entry in that Datagridview, that text cell gets somehow transformed into a textbox that can also use SpellCheck.

And, I suppose, the best thing to do would have the misspelled words be underlined in red even when the cells in that column are NOT being edited.

Is there any way to do this without having to give up using Windows Forms for this app? (It's a huge, older app here.)

Not sure if I could override the DataGridView's Editing control in a way that links-in the Spellcheck textbox that we can create via Hans' class?

Community
  • 1
  • 1
DaveyBoy
  • 435
  • 4
  • 20
  • 1
    I've never tried it but you may want to look at http://msdn.microsoft.com/en-us/library/7tas5c80(v=vs.80).aspx . You could try to create some sort of `WpfTextBoxHostedColumn` and use it for your data. But even if it is possible, it could probably lead to performance problems on huge datasets. – Eugene Podskal Jul 28 '14 at 15:47

1 Answers1

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);
        }

    }
}

This could be helped.

shankar.parshimoni
  • 1,289
  • 5
  • 22
  • 42