locked
Solution Explorer Custom Context Menu for Specific File Types RRS feed

  • Question

  • I am using Visual Studio 2013, and would like to edit the right-click context menu for the Solution Explorer. I would like the command(s) that I add to be available only when right-clicking specific file types (*.vb, *.txt, *.js, etc.). Is there any way to specify what file types a command will be available for? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Tuesday, June 2, 2015 8:47 PM

Answers

  • No, I don't think we can specify the file types with on the "External Tools". Visual Studio doesn't provide such kind of ability for the user.  We have to extend the Visual Studio Commands to execute the .bat file for some specified file types.

    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.

    Friday, June 5, 2015 5:05 AM

All replies

  • Hi Nathan Sokalski,

    I help you move this case to the VS extension forum for dedicated support. Thanks for your understanding.

    Best Regards,

    Jack


    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, June 3, 2015 8:21 AM
  • Hi Nathan,

    You need to change the vsct file of the VSPackage project to add the command menu to the Project Item's context menu. Then in the BeforeQueryStatus event handler, determine if it's the right file type to show or hide the command menu. The following is a full example:

    1. Create a VSPackage project,      select "Menu Command" in the wizard
    2. Autoload this package when      solution exists:

    HOWTO: Autoload a Visual Studio package

    [ProvideAutoLoad("f1536ef8-92ec-443c-9ed7-fdadf150da82")]

        public sealed class   MSDNCommandDemoPackage : Package

        {

    ……

    1. Open the vsct file, change      the menu group parent id:

    <Group   guid="guidMSDNCommandDemoCmdSet" id="MyMenuGroup"   priority="0x0600">

            <!--<Parent   guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS"/>-->

            <Parent   guid="guidSHLMainMenu" id="IDM_VS_CTXT_ITEMNODE"/>

          </Group>

    1. In the Package's Initialize      method, change the code to use OleMenuCommand instead of MenuCommand, so      that we can subscribe the BeforeQueryStatus event handler:

    How to: Create and Handle Commands in VSPackages (C#)

    This event will be triggered before the command shows. We'll determine if the right clicked project item is of the specified type. If it is, show the command.

    protected   override void Initialize()

            {

                Debug.WriteLine   (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of:   {0}", this.ToString()));

                base.Initialize();

                dte2 = GetService(typeof(SDTE))   as DTE2;

                // Add our command handlers for   menu (commands must exist in the .vsct file)

                OleMenuCommandService mcs =   GetService(typeof(IMenuCommandService)) as OleMenuCommandService;

                if ( null != mcs )

                {

                    // Create the command for the   menu item.

                    CommandID menuCommandID = new   CommandID(GuidList.guidMSDNCommandDemoCmdSet,   (int)PkgCmdIDList.cmdidMSDNCommandDemo);

                   

                    //MenuCommand menuItem = new   MenuCommand(MenuItemCallback, menuCommandID);

                    //mcs.AddCommand(menuItem);

                    OleMenuCommand oleMenuItem = new OleMenuCommand(new   EventHandler(MenuItemCallback), menuCommandID);

                      oleMenuItem.BeforeQueryStatus += new   EventHandler(OnBeforeQueryStatus);

                      mcs.AddCommand(oleMenuItem);

                }

            }

            void   OnBeforeQueryStatus(object sender, EventArgs e)

            {

                  var myCommand = sender as OleMenuCommand;

                  object[]   selectedItems=(object[])dte2.ToolWindows.SolutionExplorer.SelectedItems;

                  foreach (EnvDTE.UIHierarchyItem selectedUIHierarchyItem in   selectedItems)

                {

                      if (selectedUIHierarchyItem.Object is EnvDTE.ProjectItem)

                      {

                          EnvDTE.ProjectItem item = selectedUIHierarchyItem.Object as   EnvDTE.ProjectItem;

                          if (item.Name.EndsWith(".cs"))

                          {

                            //myCommand.Enabled =   true;

                            myCommand.Visible =   true;

                          }

                          else

                          {

                            //myCommand.Enabled =   false;

                            myCommand.Visible =   false;

                          }

                      }

                }

            }

    To get the selected item, see this article:

    HOWTO: Get the selected nodes in the Solution Explorer from a Visual Studio add-in

    We only shows the command when right click a C# class file(.cs file), or else the command menu is invisible.


    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.

    Thursday, June 4, 2015 4:36 AM
  • I think you misunderstood my question. I am not creating a package, extension, or any other type of project in Visual Studio, I am simply adding a command by going to Tools->External Tools to add a command that runs a *.bat file. Can I specify what file types these commands are available for? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Friday, June 5, 2015 12:38 AM
  • No, I don't think we can specify the file types with on the "External Tools". Visual Studio doesn't provide such kind of ability for the user.  We have to extend the Visual Studio Commands to execute the .bat file for some specified file types.

    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.

    Friday, June 5, 2015 5:05 AM