5
Vote

Dynamic Context Menus

description

Support owner-draw context menus by implementing:

IContextMenu2 - http://msdn.microsoft.com/en-us/library/windows/desktop/bb776091
IContextMenu3 - http://msdn.microsoft.com/en-us/library/windows/desktop/bb776086

This would allow the context menu item text to be modified based on the selected item(s).

comments

Coder13579 wrote Jul 23, 2015 at 4:21 PM

There is a workaround. Keep a reference to your menu or sub items you wish to modify and update them in the CanShowMenu function.
class myclass : SharpContextMenu
{
    ContextMenuStrip menu;

    public myclass()
    {
        menu = new ContextMenuStrip();
        var item = new ToolStripMenuItem
        {
            Text = "not going to see this"
        };
        menu.Items.Add(item);
    }

    protected override ContextMenuStrip CreateMenu()
    {
        return menu;
    }

    protected override bool CanShowMenu()
    {
        menu.Items.Clear();

        var newItem = new ToolStripMenuItem
        {
            Text = "This one will show up though"
        };
        menu.Items.Add(newItem);
    }
}