Visual Studio Add in Menu Not Showing Up RRS feed

  • Question

  • I am using the following code to add a menu to visual studio here is the complete function (see below)
    it works fine; the problem is when i take it to a test station (I mean when I move the dll), when I first open visual studio it shows the menu, then when I close visual studio and open it back, the menu does not show up
    if I change the code when I put the menu under the tools menu, there is not problem
    is there a way to fix that. I know by default visual studio add add in command to the tools menu, there must be a way to fix that

    however it works fine if I reopen visual studio with the resetaddin flag.  Is there a way that flag can be used inside the onconnect in order to solve this problem

    public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
    			_applicationObject = (DTE2)application;
    			_addInInstance = (AddIn)addInInst;
    			if(connectMode == ext_ConnectMode.ext_cm_UISetup)
    				object []contextGUIDS = new object[] { };
    				Commands2 commands = (Commands2)_applicationObject.Commands;
    				string testMenuName = "Test";
    				//Place the command on the tools menu.
    				//Find the MenuBar command bar, which is the top-level command bar holding all the main menu items:
    				CommandBar menuBarCommandBar = ((CommandBars)_applicationObject.CommandBars)["MenuBar"];
    				//Find the Test command bar on the MenuBar command bar. This is the "before" parameter to add new command bar:
    				CommandBarControl testControl = menuBarCommandBar.Controls[testMenuName];                                
    				//This try/catch block can be duplicated if you wish to add multiple commands to be handled by your Add-in,
    				//  just make sure you also update the QueryStatus/Exec method to include the new command names.
                        //Create new MyAddin8 command bar
                        CommandBarControl myControl = (CommandBarControl)menuBarCommandBar.FindControl(MsoControlType.msoControlPopup, System.Type.Missing, "MyAddin8", true, true);
                        if (myControl == null)
                            //Create new MyAddin8 command bar
                            myControl = menuBarCommandBar.Controls.Add(Type: MsoControlType.msoControlPopup, Id: 1234567890, Before: testControl.Index);
                            myControl.Caption = "MyAddin8";
                        CommandBarPopup myControlPopup = (CommandBarPopup)myControl;
    					//Add a command to the Commands collection:
    					Command command = commands.AddNamedCommand2(_addInInstance, "MyCommand1", "MyCommand1", "Executes the command for MyCommand1", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported+(int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
    					//Add a control for the command to the tools menu:
    					if((command != null) && (myControl != null))
                        //Add another command to the Commands collection:
                        Command command2 = commands.AddNamedCommand2(_addInInstance, "MyCommand2", "MyCommand2", "Executes the command for MyCommand2", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                        //Add a control for the command to the tools menu:
                        if ((command2 != null) && (myControl != null))
                            command2.AddControl(myControlPopup.CommandBar, 2);
    					//If we are here, then the exception is probably because a command with that name
    					//  already exists. If so there is no need to recreate the command and we can 
                        //  safely ignore the exception.

    • Edited by Jean Dolphin Thursday, February 16, 2012 9:23 PM
    Thursday, February 16, 2012 8:11 PM


  • Note the comment on the 'Temporary' parameter of CommandBarControls.Add:

    [in, optional] A Boolean indicating whether the new control will be
    present in future sessions of Visual Studio. Controls added with this method
    never persist beyond the current session, regardless of the setting of this

    This would mean you need to add the control everytime, not just during
    connectMode == ext_ConnectMode.ext_cm_UISetup.


    • Marked as answer by lucy-liu Thursday, February 23, 2012 8:26 AM
    Sunday, February 19, 2012 8:02 PM