Condividi tramite


Walkthrough: Subscribing to an Event (Visual Studio SDK)

This walkthrough explains how to create a tool window that responds to events in a running document table (RDT). A tool window hosts a user control that implements IVsRunningDocTableEvents. The AdviseRunningDocTableEvents method connects the interface to the events.

Prerequisites

This walkthrough requires Visual Studio SDK.

Subscribing to RDT Events

Create the tool window and connect it to the RDT events.

To subscribe to RDT events

  1. On the File menu, point to New and then click Project.

  2. In the New Project dialog box, expand Other Project Types and then click Extensibility Projects.

  3. In the Templates pane, click Visual Studio Integration Package.

  4. In the Location box, type the path of your VSPackage.

  5. In the Name box, type RDTExplorer for the solution and then click OK to start the wizard.

  6. On the Select a Programming Language page, select Visual C#.

  7. On the Select VSPackage Options page, select Tool Window.

  8. On the Tool Window Options page, change the Window name to "RDT Explorer" and then click Finish.

    The wizard creates the managed project, RDTExplorer, and the unmanaged resource-only project, RDTExplorerUI.

  9. Open the file, RDTExplorer/MyControl.cs in design view, and delete the Button, button1. Add a ListBox control and accept the default name, listbox1. Anchor this control to the left, top, and right.

  10. Open the file, RDTExplorer/MyControl.cs, in code view. Add the following lines to the using section at the start of the file.

    using Microsoft.VisualStudio;
    using Microsoft.VisualStudio.Shell;
    using Microsoft.VisualStudio.Shell.Interop;
    
  11. Modify the MyControl class so that it derives from both UserControl and IVsRunningDocTableEvents, as follows.

    public partial class MyControl :UserControl,   IVsRunningDocTableEvents
    
  12. Click the word, IVsRunningDocTableEvents.

    A SmartTag appears as a narrow rectangular box under the letter "I".

  13. Hover the pointer over the SmartTag.

    A drop-down list appears.

  14. Select Explicitly implement interface "IVsRunningDocTableEvents" on the drop-down list.

    The SmartTag writes a simple implementation of this interface.

  15. Replace the body of each method in the interface with the following code.

    return VSConstants.S_OK;
    
  16. Add a cookie holder to the top of the MyControl class, as follows.

       private uint dwCookie; 
    

    This variable holds the cookie that is returned by AdviseRunningDocTableEvents until the MyControl.Dispose method is called.

  17. Replace the body of the MyControl constructor with the following code.

    public MyControl()
    {
       InitializeComponent();
       IVsRunningDocumentTable RDT =      (IVsRunningDocumentTable)       Package.GetGlobalService(typeof(SVsRunningDocumentTable));   RDT.AdviseRunningDocTableEvents(this, out dwCookie);
    }
    

    The SVsRunningDocumentTable service is called to obtain an IVsRunningDocumentTable interface. The AdviseRunningDocTableEvents method connects RDT events to an object that implements IVsRunningDocTableEvents, in this case, the MyControl instance.

  18. Open the MyControlDesigner.cs file and add the following using statements just after the namespace directive.

    namespace Company.RDTExplorer
    {
        using Microsoft.VisualStudio.Shell;    using Microsoft.VisualStudio.Shell.Interop;
    
  19. Add the following code to the MyControl.Dispose method.

          components.Dispose();
       }
    }
    IVsRunningDocumentTable RDT =   (IVsRunningDocumentTable)    Package.GetGlobalService(typeof(SVsRunningDocumentTable));RDT.UnadviseRunningDocTableEvents(dwCookie);
    base.Dispose(disposing);
    

    The UnadviseRunningDocTableEvents method deletes the connection between MyControl and RDT event notification.

  20. In the MyControl.cs file, add the following line to the body of the OnBeforeLastDocumentUnlock handler, just before the return statement.

    listBox1.Items.Add("Entering OnBeforeLastDocumentUnlock"); 
    
  21. Add a similar line to the body of the OnAfterFirstDocumentLock handler and to other events that you want to see in the ListBox.

    listBox1.Items.Add("Entering OnAfterFirstDocumentLock"); 
    
  22. Build the project and start it in debug mode by pressing F5.

    Visual Studio Exp starts.

    Nota

    Two versions of Visual Studio are open at this point.

  23. On the View menu in Visual Studio Exp, point to Other Windows and then click RDT Explorer.

    The RDT Explorer dialog box appears. An empty listBox1 control appears in the dialog box.

  24. In Visual Studio Exp, open or create a solution.

    As OnBeforeLastDocument and OnAfterFirstDocument events are fired, notification of each event appears in listBox1.

See Also

Concepts

Tool Window Walkthroughs