Question

I am trying to dynamically fill the Solution right click menu with menu items from VS Package (Extension).

I have a extension menu with name "MyMenuName" and I am adding menu items there.

Here is the code I use:

        DTE2 dte = GetService(typeof (DTE)) as DTE2;

        CommandBars cmdBars = dte.CommandBars as CommandBars;

        CommandBar owner = cmdBars["MyMenuName"];

        CommandBarControl ctl1 = owner.Controls.Add(
            MsoControlType.msoControlButton, 1, string.Empty, 1, true);
        ctl1.Caption = "caption";
        ctl1.Enabled = true;

        //register on the Click event of the
        //button menu item
        CommandBarEvents buttonMenuItemHandler = (CommandBarEvents)dte.DTE.Events.CommandBarEvents[ctl1];

        buttonMenuItemHandler.Click += buttonMenuItemHandler_Click;

Menu Item is added successfully, but it Click handler never gets executed. Any ideas?

Edit: I digged some code here: https://netrepo.svn.codeplex.com/svn/trunk/AssemblyFactoryAddin/AssemblyFactoryAddin/GUIButton.cs And it seems that my code should be correct (assuming that their code works)

Était-ce utile?

La solution

Rather than working with CommandBarControl use CommandBarButton instead. E.g.:

var button = (CommandBarButton)owner.Controls.Add(MsoControlType.msoControlButton);
button.Caption = "caption";
button.Enabled = true;
button.Click += OnButtonClick;`

This will fix the issue.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top