TX Spell .NET: Creating Custom Context Menus
Custom spell checking context menus in TX Spell .NET use the SpellCheckContextMenuStrip property and MisspelledWords.GetItem method to detect the word at the mouse position. The code dynamically populates menu items with suggestions and replaces misspelled words on click.

Using context menus in TX Spell .NET is very simple: When connected to TX Text Control, the built-in context menu works out-of-the-box without implementing a single line of code. TX Text Control automatically replaces the default context menu with the built-in spell checking context menu.
But sometimes, you need to build your own menu or you want to combine two context menus. For this purpose, TX Text Control provides the property SpellCheckContextMenuStrip. This property specifies the context menu which is used when the end-user right-clicks a misspelled word.
In the Opening event of the context menu strip, we simply need to get the misspelled word at the current mouse position using the MisspelledWords.GetItem method in order to create the suggestions using TX Spell .NET's Check method. Each suggestion is used to create a new ToolStripMenuItem dynamically that is added to the custom context menu.
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
// clear all items from the context menu
contextMenuStrip1.Items.Clear();
// get the conversion rate twips vs. pixel
int dpi = (int)(1440 / textControl1.CreateGraphics().DpiX);
// get the location of the mouse
Point location = new Point(
(int)(textControl1.PointToClient(MousePosition).X * dpi),
(int)(textControl1.PointToClient(MousePosition).Y * dpi));
// get the misspelled word at the mouse locaion
word = textControl1.MisspelledWords.GetItem(location);
if (word == null)
return;
txSpellChecker1.CreateSuggestions(word.Text);
// fill up the context menu
foreach (Suggestion suggestion in txSpellChecker1.Suggestions)
{
ToolStripMenuItem item = new ToolStripMenuItem(suggestion.Text);
// attach the click handler
item.Click += new EventHandler(item_Click);
contextMenuStrip1.Items.Add(item);
}
if (contextMenuStrip1.Items.Count == 0)
contextMenuStrip1.Items.Add("No suggestions found.");
}
Finally, on clicking a menu item, the misspelled word is replaced with the selected suggestion text:
void item_Click(object sender, EventArgs e)
{
// replace the misspelled word with the clicked suggestion
textControl1.MisspelledWords.Remove(word, ((ToolStripMenuItem)sender).Text);
}
Download the Visual Studio 2008 project here. At least a TX Text Control .NET for Windows Forms 17.0 trial version and a TX Spell .NET for Windows Forms trial version is required. Happy coding!
Related Posts
Interactive Spelling Suggestions Using TX Spell .NET
TX Spell .NET generates real-time spelling suggestions suitable for touch-enabled interfaces. A sample project renders suggestion labels in a FlowLayoutPanel, and clicking a label calls…
Spell Checking, MailMerge and UserDictionaries
After a MailMerge operation, merged field text from the data source can trigger false spell-check errors in TX Spell .NET. By capturing each merged word in the FieldMerged event handler and adding…
Porting RapidSpell to TX Spell .NET
TX Spell .NET serves as a direct replacement for RapidSpell in existing projects. This migration guide covers differences in dictionary handling, dialog box integration, and spell-as-you-type…
Using the WSpell ActiveX Spelling Checker with TX Text Control
The WSpell ActiveX spelling checker from Wintertree Software integrates directly with TX Text Control to add spell checking to your application. This article provides sample source code that…
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.
