Other Controls
Every control that displays text can be spell checked with TX Spell .NET, not only TX Text Control and the RichTextBox. A grid cell, a label, a list item or the editor of another vendor: if your code can read the text and write it back, the spell checker finds the mistakes, offers suggestions and walks the user through them in its dialog. What your code has to do is the part TX Text Control would otherwise do: pass the text, and apply the corrections.
The Pattern
Any control other than TX Text Control works like the RichTextBox: your code initializes the spell checker with Create, passes the text to Check, shows the incorrect words to the user and applies each correction in the IncorrectWordChanging event. What TX Spell .NET Does and What Your Code Does lists the parts.
Check Any Control in the Dialog
This helper checks the text of any control in the spell check dialog. It takes two functions: one that reads the text of the control, and one that writes it back. The handler of IncorrectWordChanging applies each correction through them:
// Reads and writes the text of the control being checked
private Func<string> getText;
private Action<string> setText;
private void CheckInDialog(Func<string> get, Action<string> set)
{
getText = get;
setText = set;
txSpellChecker1.Check(getText());
txSpellChecker1.SpellCheckDialog.Show(this);
}
private void txSpellChecker1_IncorrectWordChanging(object sender, IncorrectWordChangingEventArgs e)
{
string text = getText();
int start = e.IncorrectWord.Start;
int length = e.IncorrectWord.Length;
if (e.Type == TXSpell.ChangeType.Replace)
{
setText(text.Remove(start, length).Insert(start, e.CorrectedWord));
}
else if (e.Type == TXSpell.ChangeType.Delete)
{
setText(text.Remove(start, length));
}
}
To check the content of a Label, call the helper with its text:
CheckInDialog(
() => Convert.ToString(label1.Content),
value => label1.Content = value);
In the constructor of the window, get the spell checker with FindResource, call its Create method and attach the handler: txSpellChecker1.IncorrectWordChanging += txSpellChecker1_IncorrectWordChanging;.
The handler is the one of Check a TextBox in the Dialog, which explains its arguments, with the text read and written through the two functions instead of a TextBox. To check several controls in one dialog, hand the dialog the next text in its closing event, as Checking Multiple Controls shows.
Code Without a Control
When there is no control at all, as in a service or a web application, the TXSpell class checks text with the same Check and CreateSuggestions methods; see Checking Text in Code. For the Document Editor of TX Text Control .NET Server, see TX Text Control .NET Server Document Editor.
Next Steps
- RichTextBox: the pattern applied to the standard controls.
- Events: everything the arguments of
IncorrectWordChangingcarry. - Spell Check Dialog: the dialog your users work with.