locked
Trying to add a command to the build menu RRS feed

  • Question

  • I'm trying to add a command to the build menu.  It works fine adding it to tools, but I get a fatal exception when I call AddControl.  Here is my code:

                    CommandBar menuBar = ( (CommandBars)ApplicationObject.CommandBars )["MenuBar"];

                    toolsPopup = (CommandBarPopup)menuBar.Controls["Build"];
                    if ( !FindCommand(commands, "BuildScript", out Cmd) )
                    {
                        Cmd = commands.AddNamedCommand2(AddInInstance,
                            "BuildScript", "Build Script", "Build changed script classes.",
                            true, 527, ref contextGUIDS,
                            (int)vsCommandStatus.vsCommandStatusSupported +
                            (int)vsCommandStatus.vsCommandStatusEnabled,
                            (int)vsCommandStyle.vsCommandStylePictAndText,
                            vsCommandControlType.vsCommandControlTypeButton);
                    }

                    if ( Cmd != null )
                    {
                        CommandBarButton B = (CommandBarButton)Cmd.AddControl(UDS_MainPopup.CommandBar, 0);
                        if ( B != null ) B.BeginGroup = true;
                    }

    Is there anything special about the build menu?

    Friday, November 26, 2010 2:06 AM

Answers

  • You can delete them with CommandBarButton.Delete or using Tools->Customize.  Do note that AddControl will ALWAYS add the control, it doesn't matter if it is already there, so if these are persistent between sessions then your code will add one every time and you will end up with 'a bunch of buttons'.

    Ryan

    Sunday, November 28, 2010 6:09 AM

All replies

  • Nope, nothing special about the build menu.  Have you tried passing 1 instead of 0 for the position?  DTE is 1 based.  If that isn't the issue perhaps passing in UDS_MainPopup and not its CommandBar property?

    Ryan

    Friday, November 26, 2010 3:38 AM
  • I got it working, however I'm running in to a weird side-effect. I now have 13 "Select All" buttons in the menu.  I'm trying to position the command directly before "Find and Replace".  Is Index not the right member var to use?  Also, any idea how I remove the extra "Select All" commands now :)

    Here is my code:

    CommandBar MenuBar = ( (CommandBars)ApplicationObject.CommandBars )["MenuBar"];
    Command Cmd;
    CommandBarPopup EditPopup = (CommandBarPopup)MenuBar.Controls[LocalizeMenuName("Edit")];
    if (EditPopup != null)
    {
      CommandBarPopup FindAndReplace = (CommandBarPopup)EditPopup.Controls["Find and Replace"];
      int Position = EditPopup.Controls.Count + 1;
      if ( FindAndReplace != null ) Position = FindAndReplace.Index;
      if ( !FindCommand(commands, "FindClassInHierarchy", out Cmd) )
      {
        Cmd = commands.AddNamedCommand2(AddInInstance,
        "FindClassInHierarchy", "Find in Hierarchy", "Find the currently open .UC class in the class hierarchy.",
        true, 25, ref contextGUIDS,
        (int)vsCommandStatus.vsCommandStatusSupported +
        (int)vsCommandStatus.vsCommandStatusEnabled,
        (int)vsCommandStyle.vsCommandStylePictAndText,
        vsCommandControlType.vsCommandControlTypeButton);
      }

      if ( Cmd != null )
      {
        CommandBarButton B = (CommandBarButton)Cmd.AddControl(EditPopup.CommandBar, Position);
        if ( B != null )
        {
          B.BeginGroup = true;
        }
      }
    }

    Sunday, November 28, 2010 1:36 AM
  • You can delete them with CommandBarButton.Delete or using Tools->Customize.  Do note that AddControl will ALWAYS add the control, it doesn't matter if it is already there, so if these are persistent between sessions then your code will add one every time and you will end up with 'a bunch of buttons'.

    Ryan

    Sunday, November 28, 2010 6:09 AM
  • Got the menu cleaned up but I still can't seem to add the command at the position.  If I do, eventually I get multiple "Select All" commands in the menu.  Adding it to the end of the popup work though.
    Tuesday, November 30, 2010 5:32 AM
  • I'm guessing you're calling AddControl multiple times. Can you add some diagnostics stuff (aka Debug.WriteLine) and verify that this is only being called once? Once the NamedCommand is created, it's around until you programatically remove it (or reset the environment), and if that AddControl is getting called more than once, you'll get multiple menu items. Can you provide more details around the code snippet. Specifically, when is it getting invoked?

    Thanks,


    Ed Dore
    Tuesday, November 30, 2010 7:31 AM