Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add menu item to default context menu

I'd like to add a menu item to the default ContextMenu of a RichTextBox.

I could create a new context menu but then I lose the spell check suggestions that show up in the default menu.

Is there a way to add an item without re-implementing everything?

like image 990
dmo Avatar asked Oct 16 '08 23:10

dmo


People also ask

How would you attach a context menu to a control?

Right-click the New menu, select Append Item from its context menu, and then set its properties in the Link to Command designer to the following: Text to Open.

What is add to Explorer context menu?

Using QAP Context menus You can add favorites to your QAP menu with the Windows Explorer context menus. Right-click or Shift+Right-click on any file or folder to show its context menu. QAP's command in this menu are recognizable by the QAP icon on the left.


1 Answers

It's not too tricky to reimplement the RichTextBox context menu with spelling suggestions, Cut, Paste, etc.

Hook up the context menu opening event as follows:

AddHandler(RichTextBox.ContextMenuOpeningEvent, new ContextMenuEventHandler(RichTextBox_ContextMenuOpening), true);

Within the event handler build the context menu as you need. You can recreate the existing context menu menu items with the following:

private IList<MenuItem> GetSpellingSuggestions()
{
    List<MenuItem> spellingSuggestions = new List();
    SpellingError spellingError = myRichTextBox.GetSpellingError(myRichTextBox.CaretPosition);
    if (spellingError != null)
    {
        foreach (string str in spellingError.Suggestions)
        {
            MenuItem mi = new MenuItem();
            mi.Header = str;
            mi.FontWeight = FontWeights.Bold;
            mi.Command = EditingCommands.CorrectSpellingError;
            mi.CommandParameter = str;
            mi.CommandTarget = myRichTextBox;
            spellingSuggestions.Add(mi);
        }
    }
    return spellingSuggestions;
}

private IList<MenuItem> GetStandardCommands()
{
    List<MenuItem> standardCommands = new List();

    MenuItem item = new MenuItem();
    item.Command = ApplicationCommands.Cut;
    standardCommands.Add(item);

    item = new MenuItem();
    item.Command = ApplicationCommands.Copy;
    standardCommands.Add(item);

    item = new MenuItem();
    item.Command = ApplicationCommands.Paste;
    standardCommands.Add(item);

    return standardCommands;
}

If there are spelling errors, you can create Ignore All with:

MenuItem ignoreAllMI = new MenuItem();
ignoreAllMI.Header = "Ignore All";
ignoreAllMI.Command = EditingCommands.IgnoreSpellingError;
ignoreAllMI.CommandTarget = textBox;
newContextMenu.Items.Add(ignoreAllMI);

Add separators as required. Add those to the new context menu's items, and then add your shiny new MenuItems.

I'm going to keep looking for a way to obtain the actual context menu though, as this is relevant to something I'll be working on in the near future.

like image 130
Donnelle Avatar answered Oct 12 '22 08:10

Donnelle