none
Binding to Visual Studio internal Commands from add-in

    Question

  • I am creating my own Add-in for Visual Studio 2005 and I want to ask, if there is some way to bind any my command in add-in to internal command in Visual Studio IDE?

    For example:
    VS2005 has command called Edit.BreakLine, and user can add shortcut for this command from IDE's Options menu. This shortcut will be running in main code editor in VS. When I create new editor in my Addin, and then if user wants use his shortcut for BreakLine in my editor he will have to add this shortcut to addin's command (for example "Addin.BreakLine"). So I  am looking for a way, how user doesn't create new shortcut but his shortcut for "Edit.BreakLine" will be running in my  addin's  editor.

    Thanks a lot!

    Darlock
    Tuesday, April 22, 2008 6:28 AM

Answers

  • This should do it:

    Code Snippet

    DTE dte = (DTE)Package.GetGlobalService(typeof(DTE));
    Events events = dte.Events;
    for (int i = 1; i < dte.Commands.Count; i++)
    {
        // find the command in DTE and subscribe to it's event
        Command command = dte.Commands.Item(i, i);
        if (command.Name == "Edit.BreakLine")
        {
            // get the event for the current command
            CommandEvents cmdEvent = events.get_CommandEvents(command.Guid, command.ID);
            // subscribe to it
            cmdEvent.AfterExecute += new _dispCommandEvents_AfterExecuteEventHandler(cmdEvent_AfterExecute)

            break;

        }
    }






    Tuesday, April 22, 2008 7:21 PM
  • Be careful with the garbage collection in C# events. See:

     

    PRB: Visual Studio .NET events being disconnected from add-in.

    http://www.mztools.com/resources_vsnet_addins.aspx

     

    About the original question, see also:

     

    HOWTO: Capturing commands events from Visual Studio .NET add-ins.

    (same link)

    Monday, April 28, 2008 11:05 AM
    Moderator

All replies

  • This should do it:

    Code Snippet

    DTE dte = (DTE)Package.GetGlobalService(typeof(DTE));
    Events events = dte.Events;
    for (int i = 1; i < dte.Commands.Count; i++)
    {
        // find the command in DTE and subscribe to it's event
        Command command = dte.Commands.Item(i, i);
        if (command.Name == "Edit.BreakLine")
        {
            // get the event for the current command
            CommandEvents cmdEvent = events.get_CommandEvents(command.Guid, command.ID);
            // subscribe to it
            cmdEvent.AfterExecute += new _dispCommandEvents_AfterExecuteEventHandler(cmdEvent_AfterExecute)

            break;

        }
    }






    Tuesday, April 22, 2008 7:21 PM
  • Be careful with the garbage collection in C# events. See:

     

    PRB: Visual Studio .NET events being disconnected from add-in.

    http://www.mztools.com/resources_vsnet_addins.aspx

     

    About the original question, see also:

     

    HOWTO: Capturing commands events from Visual Studio .NET add-ins.

    (same link)

    Monday, April 28, 2008 11:05 AM
    Moderator
  • Thanks both of you but it doesn't solve my problem. Your resolution only catches Visual Studio internal Command events and then calls a procedure, where I can do something. But I want bind my command to internal command. Look at my example with shortcuts. So if I use cathing command events I can't resolve this problem.
    Tuesday, April 29, 2008 8:42 AM