Products Technologies Demo Docs Blog Support Company

Implementing AutoText with TX Text Control and TX Spell .NET

AutoText is a powerful utility when typing lot of letters, e-mails or any other text. In a recent blog entry, I described how to solve this using the SpellCheckText event of TX Text Control. If you are using TX Spell .NET in combination with TX Text Control, the SpellCheckText event is blocked to avoid a potential interplay of two event listeners which might cause infinite loops. You can easily replace the SpellCheckText event with the Changed event in order to replace keywords. The…

Implementing AutoText with TX Text Control and TX Spell .NET

AutoText is a powerful utility when typing lot of letters, e-mails or any other text. In a recent blog entry, I described how to solve this using the SpellCheckText event of TX Text Control. If you are using TX Spell .NET in combination with TX Text Control, the SpellCheckText event is blocked to avoid a potential interplay of two event listeners which might cause infinite loops.

You can easily replace the SpellCheckText event with the Changed event in order to replace keywords. The following code shows how to do that:

Hashtable htAutoCorrect;

// create a new hash table for the replacements
htAutoCorrect = new Hashtable();
htAutoCorrect["vbb"] = "Visual Basic";
htAutoCorrect["css"] = "CSharp";

private void textControl1_Changed(object sender, EventArgs e)
{
    // check the MisspelledWord collection for entries in the
    // hash table. Replace the word immediately.
    // Shortcut (key in hash table) must be unique and
    // "misspelled" for the spell checker
    foreach (MisspelledWord word in textControl1.MisspelledWords)
    {
        if (htAutoCorrect.ContainsKey(word.Text))
        {
            int iStart =
                ((string)htAutoCorrect[word.Text]).Length - word.Length;

            textControl1.MisspelledWords.Remove(word,
                htAutoCorrect[word.Text].ToString());
            textControl1.Selection.Start += iStart;

            break;
        }
    }
}

Implementing AutoCorrect Using TX Spell .NET's Suggestions

While the above method uses a custom hash table to replace keywords with it's fixed values, you might want to replace misspelled words automatically on a specific action such as pressing {CTRL}+{SPACE}. The following code shows how to trap the keystrokes, get the misspelled word at the current text position and how to replace this misspelled word with the first suggestion from TX Spell .NET:

bool bActionKeys = false;

private void textControl1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Control && e.KeyCode == Keys.Space)
    {
        bActionKeys = true;
    }
}

private void textControl1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (bActionKeys == true)
    {
        e.Handled = true;
        bActionKeys = false;

        MisspelledWord word = textControl1.MisspelledWords.GetItem(
            textControl1.InputPosition.TextPosition);

        txSpellChecker1.CreateSuggestions(word.Text);

        if (txSpellChecker1.Suggestions.Count == 0)
            return;

        int iStart = txSpellChecker1.Suggestions[0].Text.Length - word.Length;

        textControl1.MisspelledWords.Remove(
            word, txSpellChecker1.Suggestions[0].Text);

        if(iStart > 0)
            textControl1.Selection.Start += iStart;
        else
            textControl1.Selection.Start -= iStart;
    }
}

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

Windows FormsWPF.NET

Create a Table of Contents in Windows Forms using C#

This article explains how to create a table of contents in Windows Forms using the ribbon or programmatically. Creating a table of contents is required to organize large documents.


ASP.NETWindows FormsWPF

Official TX Text Control .NET Sample Applications Are Now Hosted on GitHub

This article gives a quick overview of the new repositories, their structure and our plans for the future.


ASP.NETJavaScriptDocument Editor

Detect Toggle Button Changes Using a MutationObserver

This article shows how to detect changes of toggle buttons in the ribbon of the web editor using a MutationObserver. The state of a toggle button in the ribbon visualizes the state of a certain…


Windows FormsList.NET

Two Ways to Restart Numbered Lists in TX Text Control

In TX Text Control, numbered lists are continued by default and need to be reset when required. There is more than one way if you want to restart numbered lists in a document. In this article, two…


Windows FormsSampleShortcuts

Zoom Tricks: Disabling CTRL + MOUSE WHEEL and More

This article shows how to disable CTRL + MOUSE WHEEL, implement zooming with keyboard and reset the zoom factor to its default value.