Incorrect Words and Suggestions
Behind every red underline and every entry of the spell check context menu are two lists: the words the spell checker found incorrect, and the suggestions it created for one of them. Your code can read both, to build a correction interface of its own, to report the mistakes in a text, or to replace a word with its best suggestion. What follows covers where the lists come from, what each entry holds, and how TX Text Control keeps its own list of misspelled words.
How the Two Lists Are Filled
A TXSpellChecker fills its lists on request, and each request replaces what the previous one found:
| You call | It fills | With |
|---|---|---|
| Check | IncorrectWords | An IncorrectWordCollection: every incorrect word of the text you passed |
| CreateSuggestions | Suggestions | A SuggestionCollection: the suggestions for one word, closest match first |
Both use the dictionaries in Dictionaries. Check must not be called while the spell check dialog is open; it throws an exception.
A TextControl of TX Text Control that is connected to the spell checker does not use these two lists for its underlines. It keeps its own collection of misspelled words, described below.
Find the Incorrect Words of a Text
This button handler checks the text of a TextBox and lists each incorrect word with its position in a ListBox. The form has a TXSpellChecker called txSpellChecker1 from the toolbox:
private void buttonCheck_Click(object sender, EventArgs e)
{
txSpellChecker1.Check(textBox1.Text);
listBoxWords.Items.Clear();
foreach (TXTextControl.Proofing.IncorrectWord incorrectWord in txSpellChecker1.IncorrectWords)
{
listBoxWords.Items.Add($"{incorrectWord.Text} at {incorrectWord.Start}, {incorrectWord.Length} characters");
}
labelCount.Text = $"{txSpellChecker1.IncorrectWords.Count} incorrect word(s)";
}
The code works as follows:
Check(textBox1.Text)checks the text against the dictionaries. An overload,Check(string, CultureInfo), also uses the dictionaries of the default dictionary folder that match the language you pass.- Each entry is an IncorrectWord. Its Text, Start and Length say which word it is and where.
Startcounts from 0, sotextBox1.Select(incorrectWord.Start, incorrectWord.Length)selects the word. IncorrectWords.Countis the number of incorrect words; 0 means the text is correct.
An IncorrectWord has two more properties:
- IsDuplicate is
truewhen the word is spelled correctly but repeats the word in front of it, as in "the the". - Language is the language the word was checked in, or
nullwhen no specific language was set.
The Check simple text sample of the setup uses the count to tell the user whether a text box holds mistakes:

Create Suggestions for a Word
CreateSuggestions takes one word and fills Suggestions. With a maximum, it stops after that many suggestions; without one, it creates all it can find:
private void listBoxWords_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBoxWords.SelectedIndex < 0) return;
TXTextControl.Proofing.IncorrectWord incorrectWord = txSpellChecker1.IncorrectWords[listBoxWords.SelectedIndex];
// The five closest matches for the selected word
txSpellChecker1.CreateSuggestions(incorrectWord.Text, 5);
listBoxSuggestions.Items.Clear();
foreach (TXTextControl.Proofing.Suggestion suggestion in txSpellChecker1.Suggestions)
{
listBoxSuggestions.Items.Add(suggestion.Text);
}
}
The code works as follows:
- The indexer of
IncorrectWordscounts from 0, like the list box, so the selected line and the word match. CreateSuggestions(incorrectWord.Text, 5)creates the five best suggestions. The collection is sorted by closeness, soSuggestions[0]is the best one.- Each Suggestion has one property, Text.
CreateSuggestions has four overloads:
| Overload | Suggestions from |
|---|---|
CreateSuggestions(string incorrectWord) | The selected dictionaries, all suggestions |
CreateSuggestions(string incorrectWord, int max) | The selected dictionaries, at most max suggestions |
CreateSuggestions(string incorrectWord, CultureInfo language) | The selected dictionaries and those of the default dictionary folder in language |
CreateSuggestions(string incorrectWord, int max, CultureInfo language) | The same, at most max suggestions |
The suggestions take the keyboard into account, so a key next to the intended one counts as a likely typo. Since version 10.0, they also use the replacement rules, the REP flag, of a Hunspell dictionary, which map common misspellings, such as a missing accent or "f" for "ph", to the correct spelling; Version 10.0 describes the change.
Choose Which Dictionaries Check and Suggest
Every Dictionary in Dictionaries has two switches, both true by default:
- IsSpellCheckingEnabled decides whether
Checkuses the dictionary. - IsGetSuggestionsEnabled decides whether
CreateSuggestionsuses it.
A dictionary of technical terms can therefore accept words during the check without filling the suggestions with them. Loading and Managing Dictionaries covers adding dictionaries.
Work With the Misspelled Words of TX Text Control
A TextControl with a connected spell checker keeps the words it underlines in MisspelledWords, a MisspelledWordCollection of the text part that has the input focus: the main text, a header or footer, or a text frame. Its entries are MisspelledWord objects, which differ from an IncorrectWord in a few ways:
IncorrectWord | MisspelledWord | |
|---|---|---|
| Belongs to | TX Spell .NET, after Check | TX Text Control, kept up to date as the user types |
| First character | Start counts from 0 | Start counts from 1 |
| Collection index | From 0 | From 1 |
| Can be marked as ignored | No, it has no such property | Yes, IsIgnored keeps it without the underline |
To correct a word in the editor, get it from the collection, create suggestions for its text, and pass the replacement to Remove. This handler replaces the misspelled word at the input position with its best suggestion:
private void buttonCorrect_Click(object sender, EventArgs e)
{
// The misspelled word at the input position, or the next one
TXTextControl.MisspelledWord misspelledWord = textControl1.MisspelledWords.GetItem();
if (misspelledWord == null) return;
txSpellChecker1.CreateSuggestions(misspelledWord.Text, 1);
if (txSpellChecker1.Suggestions.Count > 0)
{
// Replace the text of the word with the suggestion
textControl1.MisspelledWords.Remove(misspelledWord, txSpellChecker1.Suggestions[0].Text);
}
}
The code works as follows:
- GetItem without arguments returns the misspelled word at the input position. If there is none, it returns the next one, and after the last word it starts again at the first. Another overload takes a mouse location in pixels, for a word under the pointer.
Remove(misspelledWord, text)replaces the word's text with the suggestion.Remove(misspelledWord)without a text only drops the word from the collection and leaves the text as it is.
The spell check context menu offers the same kind of correction for the word the user right-clicked:

The blog article Interactive Spelling Suggestions Using TX Spell .NET shows the suggestions in a panel of labels and replaces the misspelled word at the input position with MisspelledWords.Remove when the user clicks one.
The same calls correct words automatically while the user types. The blog article AutoCorrect Using TX Text Control and TX Spell .NET handles the KeyPress event of the TextControl: when the user types a delimiter such as a space or a period right after a misspelled word, it creates one suggestion and replaces the word with it through MisspelledWords.Remove. A word the user changes back is left alone the second time.
Check Text Without a User Interface
The TXSpell class has the same Check, IncorrectWords, CreateSuggestions and Suggestions members, without the dialogs, the context menus and the events. It suits code that only checks text, such as a validation in a background task, and it must be initialized with Create first. Checking Text in Code introduces TXSpell step by step.
Next Steps
- As-You-Type Checking: let TX Text Control underline misspelled words while the user types.
- Ignore Settings: decide which words
Checkskips, and how duplicated words are reported. - Spelling Correction Modes and UI Provider: bind a correction form of your own to the current word and its suggestions.
- Suggestion Controls: set up the suggestion lists of the dialog and the context menu, or show suggestions in a
ListBox.