locked
Adding to MVC View Editor Context Menu RRS feed

  • Question

  • Hi,

    Have written an add-in originally for visual studio 2005, which has been updated to work with VS2010, VS2012 and VS2013.

    I'm now at the point where I would like to add buttons to the MVC view editor context menu.

    I've enabled VSIPLogging, and the right click brings back the Name 'HTML Context'

    However it doesn't appear.

    Below is some example code, that works for the 'ASPX Context', but not for the 'HTML Context'

    object []contextGUIDS = new object[] { };
    Commands2 commands = (Commands2)_applicationObject.Commands;
    // Set the toolMenu to HTML Context and it doesn't work
    string toolsMenuName = "HTML Context";
    // Set the toolMenu to ASPX Context and it does work
    //string toolsMenuName = "ASPX Context";

    Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)[toolsMenuName];

    try
    {
     //Add a command to the Commands collection:
     Command command = commands.AddNamedCommand2(_addInInstance, "MyAddin1", "MyAddin1", "Executes the command for MyAddin1", 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) && (menuBarCommandBar != null))
     {
      menuBarCommandBar.Controls.Add(Microsoft.VisualStudio.CommandBars.MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, 1, true);
     }
    }

    catch(System.ArgumentException)
    {
     //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.
    }

    Many thanks for any help on this.

    Cheers

    James

    Sunday, January 11, 2015 4:24 AM

Answers

  • I tried this a bit ago, and noticed I don't even have that context menu in my retail environment where I previously disabled a bunch of extensions. In my experimental hive the HTML Context menu is there.

    My addin threw an exception when I tried to retrieve the "HTML Context" commandbar, so that's consistent with what I was setting in my retail environment.

    I did a quick check and did find a couple of HTML Context menu's defined, so I'm still somewhat confident this is likely due to a menu mixup here.

    I predicted this problem might occur way back in the VS 2005 timeframe. You may want to consider using the IVsProfferCommands scenario as outlined in this old blog entry I wrote for Dr. eX.

       Using IVsProfferCommands to retrieve a Visual Studio CommandBar.

    While the macro feature is gone, so you can't run the macros any longer, the underlying issue still persists to this day. And IVsProfferCommands is still the only fullproof way to return a specific CommandBar when context menus have the same automation name.

    Sincerely,


    Ed Dore

    • Marked as answer by Caillen Wednesday, February 4, 2015 5:21 AM
    Monday, January 19, 2015 10:48 PM

All replies

  • Hi James,

    I did a little change to your code and it works fine to add a command button to the .cshtml and .aspx code editor's context menu.

    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;
                    // Set the toolMenu to HTML Context and it doesn't work
                    string toolsMenuName = "HTML Context";
                    // Set the toolMenu to ASPX Context and it does work
                    //string toolsMenuName = "ASPX Context";
    
                    Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)[toolsMenuName];
    
                    try
                    {
                        //Add a command to the Commands collection:
                        Command command = commands.AddNamedCommand2(_addInInstance, "MyAddin1", "MyAddin1", "Executes the command for MyAddin1", true, 59,
                         ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                         (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeMRUButton);
    
                        //Add a control for the command to the tools menu:
                        if ((command != null) && (menuBarCommandBar != null))
                        {//msoControlPopup
                            //menuBarCommandBar.Controls.Add(Microsoft.VisualStudio.CommandBars.MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, 1, true);
                            command.AddControl(menuBarCommandBar, 1);
                        }
                    }
    
                    catch (System.ArgumentException)
                    {
                        //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.
                    }
    			}
    		}


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, January 13, 2015 8:20 AM
  • Hi Caillen,

    I was just wondering what version of Visual studio, you were using?

    As I have now got it working in Visual Studio 2012, but not 2013...and beginning to think it might have something to do with the peek function.

    Cheers

    James

    Tuesday, January 13, 2015 9:15 AM
  • Interestingly, if you go Tools -> Customize, then you can see the buttons, they just don't display in VS2013:

    Tuesday, January 13, 2015 9:26 AM
  • Hi Caillen,

    I was just wondering what version of Visual studio, you were using?

    As I have now got it working in Visual Studio 2012, but not 2013...and beginning to think it might have something to do with the peek function.

    Cheers

    James

    Yes I tested with VS2012. Have not tested with VS2013, but will do.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 14, 2015 6:01 AM
  • Hi James,

    VS 2013 Introduced a new HTML editor into the mix:

    http://blogs.msdn.com/b/webdev/archive/2013/06/27/html-editing-features-in-visual-studio-2013-preview.aspx

    I suspect that if you switch back and open the file with the older webform editor, you'll find your menu items. To add these to the context menu of the new editor, you'll need to identify the context menu via something like the EnableVSIPLogging flag, and add the menu there as well.

    Sincerely,


    Ed Dore

    Monday, January 19, 2015 6:15 AM
  • Hi Ed,  

    I've enabled EnableVSIPLogging and it still comes back as HTML Context.

    I've looked at all the commands that in the HTML Context and it seems to match those that are displayed, so i'm pretty sure it's that menu.

    Cheers

    James

    Monday, January 19, 2015 9:52 AM
  • I tried this a bit ago, and noticed I don't even have that context menu in my retail environment where I previously disabled a bunch of extensions. In my experimental hive the HTML Context menu is there.

    My addin threw an exception when I tried to retrieve the "HTML Context" commandbar, so that's consistent with what I was setting in my retail environment.

    I did a quick check and did find a couple of HTML Context menu's defined, so I'm still somewhat confident this is likely due to a menu mixup here.

    I predicted this problem might occur way back in the VS 2005 timeframe. You may want to consider using the IVsProfferCommands scenario as outlined in this old blog entry I wrote for Dr. eX.

       Using IVsProfferCommands to retrieve a Visual Studio CommandBar.

    While the macro feature is gone, so you can't run the macros any longer, the underlying issue still persists to this day. And IVsProfferCommands is still the only fullproof way to return a specific CommandBar when context menus have the same automation name.

    Sincerely,


    Ed Dore

    • Marked as answer by Caillen Wednesday, February 4, 2015 5:21 AM
    Monday, January 19, 2015 10:48 PM