Products Technologies Demo Docs Blog Support Company

TX Spell .NET: Creating Custom Context Menus

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…

TX Spell .NET: Creating Custom Context Menus

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!

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

SampleSpell Checking

Interactive Spelling Suggestions Using TX Spell .NET

With the introduction of touch-enabled interfaces in many applications, new requirements are coming up. Users expect tailored interfaces for various scenarios. In a touch-enabled word processor…


SampleSpell Checking

Spell Checking, MailMerge and UserDictionaries

Today, we received an interesting request from a TX Text Control user. The requirement was straightforward: A template should be prepared by merging data using the MailMerge component. The…


SampleSpell CheckingTutorial

Porting RapidSpell to TX Spell .NET

Our new spell checking component TX Spell .NET can be easily used to replace RapidSpell in your projects. Integrating TX Spell .NET into TX Text Control based applications is as easy as 1-2-3.…


SampleSpell Checking

Using the WSpell ActiveX Spelling Checker with TX Text Control

A brief article, with sample source code, that shows you how to add spell checking capabilities to your TX Text Control application, using the popular ActiveX spell checker, WSpell, from Wintertree.


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.