locked
How to add shortcuts for dynamically created menu commands? RRS feed

  • Question

  • Hi,

    I have created shortcut for menu commands in VsPackage.

    I have created dynamic menu items using OleMenuCommand. How can i add a shortcut for a dynamically created menu item in ctc file?

    Any example code would help me...

    Thanks,

     

     

    Nayna

    Friday, January 14, 2011 4:36 AM

Answers

  • I would have to look at the code, we may not allow binding to commands which we have no backing command data for. Out of curiosity why are you trying to define commands like this that aren't present in the CTC?  I don't think VS will be able to dispatch these commands since the code expect to be able to find a shell command object and to find such a thing it needs to do a CTM lookup.

    Ryan

    • Marked as answer by Victor_Chen Tuesday, January 25, 2011 9:28 AM
    Tuesday, January 18, 2011 7:04 AM

All replies

  • What do you mean by 'dynamically created commands' here?  Are you talking about an MRU style list of DynamicItemStart items?  Or are you talking about run-time created commands ala IVSProfferCommands/DTE.Commands?

    Ryan

    Friday, January 14, 2011 4:09 PM
  • Hi,

    Thanks for the reply.

    I have created submenu commands in the following way:

    private void CreateC_M_SubMenuCommandHandlers(OleMenuCommandService mcs)
        {
          int baseCommandID = (int)PkgCmdIDList.cmdidComp;
    
          CommandID IDCommand1 = new CommandID(GuidList.guidReCmdSet, baseCommandID);
          OleMenuCommand dynamicCmd1 = new OleMenuCommand(new EventHandler(FindComp), IDCommand1);
          dynamicCmd1.Text = "Generate M_";
          mcs.AddCommand(dynamicCmd1);
    
          CommandID IDCommand2 = new CommandID(GuidList.guidReCmdSet, baseCommandID + 1);
          OleMenuCommand dynamicCmd2 = new OleMenuCommand(new EventHandler(GenerateReport), IDCommand2);
          dynamicCmd2.Text = "Generate Report";
          mcs.AddCommand(dynamicCmd2);
    
          CommandID IDCommand3 = new CommandID(GuidList.guidReCmdSet, baseCommandID + 2);
          OleMenuCommand dynamicCmd3 = new OleMenuCommand(new EventHandler(ClearViolations), IDCommand3);
          dynamicCmd3.Text = "Clear Violations";
          mcs.AddCommand(dynamicCmd3);
        }
    

    Now how should i add shortcut's to them in ctc file? Let me know if i need to provide more information.

    Thanks,

    Nayna

    Monday, January 17, 2011 3:24 AM
  • You should be using VSCT not CTC, and you want to put a KeyBinding element for each command.

    Ryan

    Monday, January 17, 2011 4:30 AM
  • Hi,

    I have created a VSPackage in Visual studio 2005. So it is using ctc file for command creation.

    I have added the keyBinding for the parent menu command. Now for commands created during runtime, how should the shortcuts be added?

    Let me know if you need more information.

    Thanks,

    Nayna

    Monday, January 17, 2011 5:11 AM
  • What you have is fine, but I don't see why you don't just have CTC elements for those items since they are just hard coded into your code behind anyways.  You can add keybindings for any GUID/DWORD pair, so you can add them for guidReCmdSet, baseCommandID+2 for instance, though you can't say baseCommandID+2 in the CTC file, we don't support expressions with the defined constants, but just put the literal value in there, so if baseCommandID was 100 then you would have a KeyBinding element with the GUID of guidReCmdSet and an id of 102.

    Ryan

    Monday, January 17, 2011 5:47 AM
  • Hi,

    I tried with the method you suggested.

    Now i have updated the Key Bindings section as shown below:

    guidReViewerCmdSet:0x112, guidVSStd97,  guidVSStd97,  'G':A;
    

    But the shortcut doesn't work :(

    Thanks,

    Nayna 

    Monday, January 17, 2011 6:30 AM
  • I know nothing about CTC as it isn't the format used for command anymore.  Does it recognize hex based number?  Have you tried using base10 numbering for your ID?  Does your binding conflict with an existing binding?  You can't bind more than one command to the same key chord in the same scope.  What is 'G':A here?  As I said I am unfamilar with CTC so it isn't clear what your attempted key chord is just based on the above.

    Ryan

    Monday, January 17, 2011 5:18 PM
  • I tried with base 10 numbering, and there is no conflict with command IDs which i have given.

    'G':A mean "Alt"+G - shortcut key for one of the commands. I will try doing the same in visual studio 2008 and get back.

    Thanks for the help.

     

    Nayna

    Tuesday, January 18, 2011 3:51 AM
  • I would have to look at the code, we may not allow binding to commands which we have no backing command data for. Out of curiosity why are you trying to define commands like this that aren't present in the CTC?  I don't think VS will be able to dispatch these commands since the code expect to be able to find a shell command object and to find such a thing it needs to do a CTM lookup.

    Ryan

    • Marked as answer by Victor_Chen Tuesday, January 25, 2011 9:28 AM
    Tuesday, January 18, 2011 7:04 AM