Context menu on right click

SyntaxEditor for Windows Forms Forum

Posted 12 years ago by Kelly Leahy - Software Architect, Milliman
Version: 4.0.0249
Avatar
I would like to add a context menu to the editor in my application, but I need most of the items in the context menu to be context sensitive (no pun intended).

For instance, if a user right clicks on a selection, I'd like cut/copy/paste/etc. to show up on the menu, if they right click with no selection I'd like paste to show up, along with other items...

If the item they right click is an identifier (formula name), I'd like to show them some choices for what to do with that identifier - for instance, "open formula in another window", "browse to formula", ... If the item is something weird like whitespace or an operator, I'd like to show only the clipboard ops.

How do you recommend is the best architecture for this - should I use one shared context menu and an event handler to filter the menu based on the right click location? Is there even an event for me to use for this?

Can I do what I'm looking for using the ContextMenuStrip property (and DefaultContextMenuEnabled = false)? What event should I hook to do the filtering?

Thanks,

Kelly Leahy Software Architect Milliman, USA

Comments (4)

Posted 12 years ago by Kelly Leahy - Software Architect, Milliman
Avatar
I just found ContextMenuRequested. Is this event fired when ContextMenuStrip is needed too, or is it just for .NET 1.1 style context menus?

Kelly Leahy Software Architect Milliman, USA

Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
It fires whenever the control receives a WM_CONTEXTMENU message.

[Modified at 05/14/2007 02:49 PM]


Actipro Software Support

Posted 12 years ago by Kelly Leahy - Software Architect, Milliman
Avatar
Ok... I got this working, but I was wondering if there was another way you recommend doing this, or if this is the "best" way you know?

My technique is:
1) catch the ContextMenuRequested event and modify the shared ContextMenuStrip to contain the items that are applicable for the "context", computing the context by doing something like the following:
int ofs = _Editor.SelectedView.LocationToOffset(e.MouseLocation, LocationToPositionAlgorithm.Block);
            if (ofs != -1)
            {
                IToken tok = _Editor.Document.Tokens.GetTokenAtOffset(ofs);
                if (tok != null)
                {
                    if (tok.ID == FormulaTokenID.FormulaId)
                    {
                        string varname = _Editor.Document.GetSubstring(tok.TextRange);
                        if (!_IsFullWindow)
                        {
                            formulaGotoVariableMenuItem.Visible = true;
                            formulaGotoVariableMenuItem.Text = "Browse to variable '" + varname + "'";
                            formulaGotoVariableMenuItem.Tag = varname;
                        }
                        formulaOpenVariableMenuItem.Visible = true;
                        formulaOpenVariableMenuItem.Text = "Open variable '" + varname + "' in full editor";
                        formulaOpenVariableMenuItem.Tag = varname;
                        formulaGotoGroupSeparator.Visible = true;
                    }
                }
            }
2. use a shared menu with event handlers attached to the menu items and simply hide the menu items that aren't applicable for a given popup context.

Does this seem like the best way to do it? Seems a bit crappy.

Kelly Leahy Software Architect Milliman, USA

Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
It's probably either that way or build a method that constructs the menu from scratch each time and use that. The ContextMenuRequested is the place to dynamically change the menu though.

You could even just keep SyntaxEditor.DefaultContextMenuEnabled = false, and don't set a ContextMenu or ContextMenuStrip to SyntaxEditor. Then in ContextMenuRequested, create your menu and show it.


Actipro Software Support

The latest build of this product (v2018.1 build 0341) was released 10 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.