Question

Tan Stanley on Mon, 16 Mar 2015 12:32:25


i am currently facing an issue, and desperately need help. any help will be very much appreciated.

in my vsct file, i do have 2 buttons. 

   <Button guid="guidMyFyp_PluginCmdSet" id="cmdidGenerateDiagram" priority="0x0100" type="Button">
        <Parent guid="guidMyFyp_PluginCmdSet" id="MyMenuGroup" />
        <Icon guid="guidImages" id="bmpPic1" />
        <Strings>
          <ButtonText>Generate Diagram</ButtonText>
        </Strings>
      </Button>

      <Button guid="guidMyFyp_PluginCmdSet" id="cmdidCreateRulesCommand" priority="0x0200" type="Button">
        <Parent guid="guidMyFyp_PluginCmdSet" id="RulesGroup" />
        <Icon guid="guidImages" id="bmpPic2" />
        <Strings>
          <CommandName>cmdidCreateRulesCommand</CommandName>
          <ButtonText>Create Rules</ButtonText>
        </Strings>
      </Button>

and i am trying to create a new command for the button with the id "cmdidCreateRulesCommand". therefore in my VSPackage.cs, i included the following in my initialize method.

  OleMenuCommandService mcs2 = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
            if (null != mcs2)
            {
          
                CommandID openRuleForm = new CommandID(GuidList.guidMyFyp_PluginCmdSet, (int)PkgCmdIDList.cmdidCreateRulesCommand);
                MenuCommand menuItem2 = new MenuCommand(MenuItemCallback, openRuleForm);
                mcs2.AddCommand(menuItem2);
            }

however upon running my program, it is not able to detect my new command to execute my method. can anyone please enlighten me? 


Sponsored



Replies

Carlos J. Quintero on Mon, 16 Mar 2015 15:47:43


Can you post the WHOLE contents of:

- .vsct file

- Guids.cs

- PkgCmdID.cs

- Package.cs

Tan Stanley on Mon, 16 Mar 2015 15:51:59


Sure Mr Carlos, below will be content:

//.vsct file

                                                             

<?xml version="1.0" encoding="utf-8"?>
<CommandTable xmlns="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <!--  This is the file that defines the actual layout and type of the commands.
        It is divided in different sections (e.g. command definition, command
        placement, ...), with each defining a specific set of properties.
        See the comment before each section for more details about how to
        use it. -->

  <!--  The VSCT compiler (the tool that translates this file into the binary 
        format that VisualStudio will consume) has the ability to run a preprocessor 
        on the vsct file; this preprocessor is (usually) the C++ preprocessor, so 
        it is possible to define includes and macros with the same syntax used 
        in C++ files. Using this ability of the compiler here, we include some files 
        defining some of the constants that we will use inside the file. -->

  <!--This is the file that defines the IDs for all the commands exposed by VisualStudio. -->
  <Extern href="stdidcmd.h"/>

  <!--This header contains the command ids for the menus provided by the shell. -->
  <Extern href="vsshlids.h"/>




  <!--The Commands section is where we the commands, menus and menu groups are defined.
      This section uses a Guid to identify the package that provides the command defined inside it. -->
  <Commands package="guidMyFyp_PluginPkg">
    <!-- Inside this section we have different sub-sections: one for the menus, another  
    for the menu groups, one for the buttons (the actual commands), one for the combos 
    and the last one for the bitmaps used. Each element is identified by a command id that  
    is a unique pair of guid and numeric identifier; the guid part of the identifier is usually  
    called "command set" and is used to group different command inside a logically related  
    group; your package should define its own command set in order to avoid collisions  
    with command ids defined by other packages. -->


    <!-- In this section you can define new menu groups. A menu group is a container for 
         other menus or buttons (commands); from a visual point of view you can see the 
         group as the part of a menu contained between two lines. The parent of a group 
         must be a menu. -->
    <Menus>

      <Menu guid="guidMyFyp_PluginCmdSet" id="MyFyp_Plugin" priority="0x700" type="Menu">
        <Parent guid="guidSHLMainMenu" id="IDG_VS_MM_TOOLSADDINS" />
        <Strings>
          <ButtonText>Fyp_Plugin</ButtonText>
          <CommandName>Fyp_Plugin</CommandName>
        </Strings>
      </Menu>

      <Menu guid="guidMyFyp_PluginCmdSet" id="RulesMenu" priority="0x0100" type="Menu">
        <Parent guid="guidMyFyp_PluginCmdSet" id="MyMenuGroup"/>
        <Strings>
          <ButtonText>Rules</ButtonText>
          <CommandName>Rules</CommandName>
        </Strings>
      </Menu>

    </Menus>

    <Groups>

      <Group guid="guidMyFyp_PluginCmdSet" id="MyMenuGroup" priority="0x0600">
        <Parent guid="guidMyFyp_PluginCmdSet" id="MyFyp_Plugin"/>
      </Group>

      <Group guid="guidMyFyp_PluginCmdSet" id="RulesGroup" priority="0x0000">
        <Parent guid="guidMyFyp_PluginCmdSet" id="RulesMenu"/>
      </Group>

    </Groups>

    <!--Buttons section. -->
    <!--This section defines the elements the user can interact with, like a menu command or a button 
        or combo box in a toolbar. -->
    <Buttons>
      <!--To define a menu group you have to specify its ID, the parent menu and its display priority. 
          The command is visible and enabled by default. If you need to change the visibility, status, etc, you can use
          the CommandFlag node.
          You can add more than one CommandFlag node e.g.:
              <CommandFlag>DefaultInvisible</CommandFlag>
              <CommandFlag>DynamicVisibility</CommandFlag>
          If you do not want an image next to your command, remove the Icon node /> -->

      <Button guid="guidMyFyp_PluginCmdSet" id="cmdidGenerateDiagram" priority="0x0100" type="Button">
        <Parent guid="guidMyFyp_PluginCmdSet" id="MyMenuGroup" />
        <Icon guid="guidImages" id="bmpPic1" />
        <Strings>
          <ButtonText>Generate Diagram</ButtonText>
        </Strings>
      </Button>

      <Button guid="guidMyFyp_PluginCmdSet" id="cmdidCreateRulesCommand" priority="0x0200" type="Button">
        <Parent guid="guidMyFyp_PluginCmdSet" id="RulesGroup" />
        <Icon guid="guidImages" id="bmpPic2" />
        <Strings>
          <CommandName>cmdidCreateRulesCommand</CommandName>
          <ButtonText>Create Rules</ButtonText>
        </Strings>
      </Button>

    </Buttons>

    <!--The bitmaps section is used to define the bitmaps that are used for the commands.-->
    <Bitmaps>
      <!--  The bitmap id is defined in a way that is a little bit different from the others: 
            the declaration starts with a guid for the bitmap strip, then there is the resource id of the 
            bitmap strip containing the bitmaps and then there are the numeric ids of the elements used 
            inside a button definition. An important aspect of this declaration is that the element id 
            must be the actual index (1-based) of the bitmap inside the bitmap strip. -->
      <Bitmap guid="guidImages" href="Resources\Images.png" usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows"/>

    </Bitmaps>

  </Commands>





  <Symbols>
    <!-- This is the package guid. -->
    <GuidSymbol name="guidMyFyp_PluginPkg" value="{4bca0020-01d6-4060-ad23-6993bd2d4fa6}" />

    <!-- This is the guid used to group the menu commands together -->
    <GuidSymbol name="guidMyFyp_PluginCmdSet" value="{b65cbf92-89cb-4f4a-8b84-6dfb5c4656b1}">

      <IDSymbol name="MyMenuGroup" value="0x1020" />
      <IDSymbol name="cmdidGenerateDiagram" value="0x0100" />

      <IDSymbol name="MyFyp_Plugin" value="0x1021"/>

      <IDSymbol name="RulesMenu" value="0x1100"/>
      <IDSymbol name="RulesGroup" value="0x1150"/>
      <IDSymbol name="cmdidCreateRulesCommand" value="0x0105"/>

    </GuidSymbol>



    <GuidSymbol name="guidImages" value="{53a55796-06b8-4bb0-8418-42d4224abce1}" >
      <IDSymbol name="bmpPic1" value="1" />
      <IDSymbol name="bmpPic2" value="2" />
      <IDSymbol name="bmpPicSearch" value="3" />
      <IDSymbol name="bmpPicX" value="4" />
      <IDSymbol name="bmpPicArrows" value="5" />
      <IDSymbol name="bmpPicStrikethrough" value="6" />
    </GuidSymbol>
  </Symbols>

</CommandTable>

Tan Stanley on Mon, 16 Mar 2015 15:54:11


//Guid.cs

// Guids.cs
// MUST match guids.h
using System;

namespace NTUFinalYearProject.MyFyp_Plugin
{
    static class GuidList
    {
        public const string guidMyFyp_PluginPkgString = "4bca0020-01d6-4060-ad23-6993bd2d4fa6";
        public const string guidMyFyp_PluginCmdSetString = "b65cbf92-89cb-4f4a-8b84-6dfb5c4656b1";

        public static readonly Guid guidMyFyp_PluginCmdSet = new Guid(guidMyFyp_PluginCmdSetString);
    };
}


// PkgCmdID.cs
// MUST match PkgCmdID.h
using System;

namespace NTUFinalYearProject.MyFyp_Plugin
{
    static class PkgCmdIDList
    {
        public const uint cmdidCreateRules =        0x100;


        public const uint cmdidCreateRulesCommand = 0x200;

    };
}

Tan Stanley on Mon, 16 Mar 2015 15:55:19


//Package.cs 

using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using Microsoft.Win32;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;

namespace NTUFinalYearProject.MyFyp_Plugin
{
    /// <summary>
    /// This is the class that implements the package exposed by this assembly.
    ///
    /// The minimum requirement for a class to be conside red a valid package for Visual Studio
    /// is to implement the IVsPackage interface and register itself with the shell.
    /// This package uses the helper classes defined inside the Managed Package Framework (MPF)
    /// to do it: it derives from the Package class that provides the implementation of the 
    /// IVsPackage interface and uses the registration attributes defined in the framework to 
    /// register itself and its components with the shell.
    /// </summary>
    // This attribute tells the PkgDef creation utility (CreatePkgDef.exe) that this class is
    // a package.
    [PackageRegistration(UseManagedResourcesOnly = true)]
    // This attribute is used to register the information needed to show this package
    // in the Help/About dialog of Visual Studio.
    [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
    // This attribute is needed to let the shell know that this package exposes some menus.
    [ProvideMenuResource("Menus.ctmenu", 1)]
    [Guid(GuidList.guidMyFyp_PluginPkgString)]
    public sealed class MyFyp_PluginPackage : Package
    {
        /// <summary>
        /// Default constructor of the package.
        /// Inside this method you can place any initialization code that does not require 
        /// any Visual Studio service because at this point the package object is created but 
        /// not sited yet inside Visual Studio environment. The place to do all the other 
        /// initialization is the Initialize method.
        /// </summary>
        public MyFyp_PluginPackage()
        {
            Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this.ToString()));
        }



        /////////////////////////////////////////////////////////////////////////////
        // Overridden Package Implementation
        #region Package Members

        /// <summary>
        /// Initialization of the package; this method is called right after the package is sited, so this is the place
        /// where you can put all the initialization code that rely on services provided by VisualStudio.
        /// </summary>
        protected override void Initialize()
        {
            Debug.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
            base.Initialize();

            // 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.guidMyFyp_PluginCmdSet, (int)PkgCmdIDList.cmdidCreateRules);
               MenuCommand menuItem = new MenuCommand(MenuItemCallback, menuCommandID );
               mcs.AddCommand( menuItem );

       
            }

            OleMenuCommandService mcs2 = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
            if (null != mcs2)
            {
          
                CommandID openRuleForm = new CommandID(GuidList.guidMyFyp_PluginCmdSet, (int)PkgCmdIDList.cmdidCreateRulesCommand);
                MenuCommand menuItem2 = new MenuCommand(MenuItemCallback, openRuleForm);
                mcs2.AddCommand(menuItem2);
            }
        }
        #endregion

        /// <summary>
        /// This function is the callback used to execute a command when the a menu item is clicked.
        /// See the Initialize method to see how the menu item is associated to this function using
        /// the OleMenuCommandService service and the MenuCommand class.
        /// </summary>
        private void MenuItemCallback(object sender, EventArgs e)
        {
             Show a Message Box to prove we were here
            IVsUIShell uiShell = (IVsUIShell)GetService(typeof(SVsUIShell));
            Guid clsid = Guid.Empty;
            int result;
            Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(uiShell.ShowMessageBox(
                       0,
                       ref clsid,
                       "MyFyp_Plugin",
                       string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this.ToString()),
                       string.Empty,
                       0,
                       OLEMSGBUTTON.OLEMSGBUTTON_OK,
                       OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST,
                       OLEMSGICON.OLEMSGICON_INFO,
                       0,        // false
                       out result));

        }

        private void OpenRulesForm(object sender, EventArgs e)
        {
            AddRules ruleForm = new AddRules();
            ruleForm.Show();
        }

    }
}


Carlos J. Quintero on Mon, 16 Mar 2015 20:56:18


Notice in bold the inconsistencies between PkgCmdIDList class and .vsct file

1) In the name for the first cmdid command
2) In the value for the second cmdid command

namespace NTUFinalYearProject.MyFyp_Plugin
{
   static class PkgCmdIDList
   {
      public const uint cmdidCreateRules = 0x100;
      public const uint cmdidCreateRulesCommand = 0x200;
   };
}
...
      <Buttons>
         <Button guid="guidMyFyp_PluginCmdSet" id="cmdidGenerateDiagram" priority="0x0100" type="Button">
          ....
         </Button>
         <Button guid="guidMyFyp_PluginCmdSet" id="cmdidCreateRulesCommand" priority="0x0200" type="Button">
          ...
         </Button>
      </Buttons>
...
      <GuidSymbol name="guidMyFyp_PluginCmdSet" value="{b65cbf92-89cb-4f4a-8b84-6dfb5c4656b1}">
   ...
         <IDSymbol name="cmdidGenerateDiagram" value="0x0100" />
  ...
         <IDSymbol name="cmdidCreateRulesCommand" value="0x0105"/>
      </GuidSymbol>

Tan Stanley on Tue, 17 Mar 2015 01:02:51


Thank you so much for your patience and help Mr Carlos, cheers.