AutoCorrect Using TX Text Control and TX Spell .NET
AutoCorrect can be very annoying on a smartphone - we all know this. But if you are used to it and then start typing on a computer keyboard, you are missing this time saving feature. TX Spell .NET provides a very powerful spell checking and suggestion engine that can be used to implement AutoCorrect with a few lines of code: Based on the analysis of thousands of test documents, the expected suggestion is ranked at position 1 or 2 in more than 97% of all cases. The algorithm includes the…

AutoCorrect can be very annoying on a smartphone - we all know this. But if you are used to it and then start typing on a computer keyboard, you are missing this time saving feature.

TX Spell .NET provides a very powerful spell checking and suggestion engine that can be used to implement AutoCorrect with a few lines of code:
string sLastStoredWordText;
private void textControl1_KeyPress(object sender, KeyPressEventArgs e)
{
// delimiter array
char[] cDelimiters = new char[] { ',','.','\r','\t',' ' };
// check, if a delimiter has been pressed and if
// misspelled words have been found
if (!cDelimiters.Contains(e.KeyChar) ||
textControl1.MisspelledWords == null ||
textControl1.MisspelledWords.Count == 0)
return;
int iTextPosition = textControl1.InputPosition.TextPosition;
// find a misspelled word at the input position
MisspelledWord mwLastWord =
textControl1.MisspelledWords.GetItem(iTextPosition);
// if the found word is not at the input position
// store the last word and return
if (mwLastWord.Start + mwLastWord.Length != iTextPosition + 1)
{
sLastStoredWordText = mwLastWord.Text; return;
}
// if the found word is the last word, store it and return
// this prevents an auto replacement for intentionally wrong words
if (mwLastWord.Text == sLastStoredWordText)
{
sLastStoredWordText = mwLastWord.Text; return;
}
// store the last word
sLastStoredWordText = mwLastWord.Text;
// create the suggestions, we only need the first
txSpellChecker1.CreateSuggestions(mwLastWord.Text, 1);
// if suggestions are found, replace the word with the suggestion
if (txSpellChecker1.Suggestions.Count > 0)
{
e.Handled = true;
string sSuggestion = txSpellChecker1.Suggestions[0].Text + " ";
textControl1.MisspelledWords.Remove(mwLastWord, sSuggestion);
textControl1.Select(mwLastWord.Start + sSuggestion.Length, 0);
}
}
Based on the analysis of thousands of test documents, the expected suggestion is ranked at position 1 or 2 in more than 97% of all cases. The algorithm includes the measurement of the distance between the keys on the currently used keyboard. Many different factors are evaluated and rated to create the accurate list of suggestions.
Essentially, the KeyPress event is used to trap keystrokes when a delimiter such as a space, a '.' or a ',' is typed in. Afer that, the misspelled word at the current input position is checked and replaced with the first suggestion of the TX Spell .NET suggestion engine.
This implementation checks also, if you intentionally typed a word incorrect. The second time you "correct" the correction, AutoCorrect is not applied to the same word again.

Happy coding!
Also See
This post references the following in the documentation:
TX Text Control .NET for Windows Forms
- TXText
Control. Misspelled Word Class
TX Spell .NET for Windows Forms
- TXText
Control. Proofing. TXSpell. Create Suggestions Method
Windows Forms
Text Control combines the power of a reporting tool and an easy-to-use WYSIWYG word processor - fully programmable and embeddable in your Windows Forms application. TX Text Control .NET for Windows Forms is a royalty-free, fully programmable rich edit control that offers developers a broad range of word processing features in a reusable component for Visual Studio.
Related Posts
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.
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.
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…
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.
Using Custom Document Properties to Store Additional Document Information
Custom properties can be used to store additional information about the document in the document itself. These properties remain with a document and can be viewed by all MS Word users that open…