Export (0) Print
Expand All

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.

To complete this walkthrough, you must install the Visual Studio 2010 SDK.

NoteNote

For more information about the Visual Studio SDK, see Visual Studio Integration SDK. To find out how to download the Visual Studio SDK, see Visual Studio Extensibility Developer Center on the MSDN Web site.

The Visual Studio Package project template can be found in three different locations in the New Project dialog:

  1. Under Visual Basic Extensibility. The default language of the project is Visual Basic.

  2. Under C# Extensibility. The default language of the project is C#.

  3. Under Other Project Types Extensibility. The default language of the project is C++.

Create a Managed VSPackage to provide a tool window

  1. Create a VSPackage. For more information, see Walkthrough: Creating a Menu Command By Using the Visual Studio Package Template. Name the package RDTExplorerVB for Visual Basic or RDTExplorerCS for Visual C#.

  2. In the Visual Studio Package Template, do the following things:

    • Set the programming language to Visual Basic or Visual C#, as appropriate for your VSPackage.

    • Use the default values on the Basic Package Information page.

    • Add a tool window that has the name RDT Explorer VB for Visual Basic or RDT Explorer CS for Visual C#.

To subscribe to RDT events

  1. Open the MyControl file in design view, and delete the button, button1. Add a ListBox control and accept the default name. Set the Dock property for the list box to Fill.

  2. Open the MyControl file in code view. Add the following statements to the start of the file.

    Imports Microsoft.VisualStudio
    Imports Microsoft.VisualStudio.Shell
    Imports Microsoft.VisualStudio.Shell.Interop
    

    using Microsoft.VisualStudio;
    using Microsoft.VisualStudio.Shell;
    using Microsoft.VisualStudio.Shell.Interop;
    
  3. Modify the MyControl class so that, in addition to deriving from the UserControl class, it implements the IVsRunningDocTableEvents interface.

    Public Class MyControl
        Inherits UserControl
        Implements IVsRunningDocTableEvents 
    

    public partial class MyControl
        : UserControl, IVsRunningDocTableEvents 
    
  4. Add method stubs for IVsRunningDocTableEvents.

    • In Visual Basic, position the cursor at the end of the line you just added, Implements IVsRunningDocTableEvents, and then press Enter.

    • In Visual C#, position the cursor in the interface name, IVsRunningDocTableEvents, click the smart tag that appears, and select Explicitly implement interface ' IVsRunningDocTableEvents'.

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

    return VSConstants.S_OK
    

    return VSConstants.S_OK;
    
  6. Add a cookie holder to the MyControl class.

    Private rdtCookie As UInteger
    

    private uint rdtCookie; 
    

    This variable holds the cookie that is returned by the AdviseRunningDocTableEvents method.

  7. Update the default constructor for the MyControl class to register for RDT events.

    Public Sub New()
    
        InitializeComponent()
    
        Dim rdt As IVsRunningDocumentTable = CType( _
            Package.GetGlobalService(GetType(SVsRunningDocumentTable)), _
            IVsRunningDocumentTable)
        rdt.AdviseRunningDocTableEvents(this, dwCookie)
    
    End Sub
    

    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, a MyControl object.

  8. Open the MyControlDesigner file, which appears as in a node under the MyControl file in Solution Explorer. If the MyControlDesigner file is not visible, on the Solution Explorer toolbar, click Show All Files. Do the following things:

    • Add the following statements to the start of the file.

      Imports Microsoft.VisualStudio.Shell
      Imports Microsoft.VisualStudio.Shell.Interop
      

      using Microsoft.VisualStudio.Shell;
      using Microsoft.VisualStudio.Shell.Interop;
      
    • Update the Dispose method.

      Protected Overrides Sub Dispose(ByVal disposing As Boolean)
          Try
              If disposing AndAlso components ISNot Nothing Then
                  components.Dispose()
              End If
      
              ' Release the RDT cookie.
              Dim rdt As IVsRunningDocumentTable = CType( _
                  Package.GetGlobalService(GetType(SVsRunningDocumentTable)), _
                  IVsRunningDocumentTable)
              rdt.UnadviseRunningDocTableEvents(rdtCookie)
      
          Finally
      
              MyBase.Dispose(disposing)
          End Try
      End Sub
      

      protected override void Dispose(bool disposing)
      {
          if (disposing)
          {
              if (components != null)
              {
                  components.Dispose();
              }
          }
      
          // Release the RDT cookie.
          IVsRunningDocumentTable rdt = (IVsRunningDocumentTable)
              Package.GetGlobalService(typeof(SVsRunningDocumentTable));
          rdt.UnadviseRunningDocTableEvents(rdtCookie);
      
          base.Dispose(disposing);
      }
      

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

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

    ListBox1.Items.Add("Entering OnBeforeLastDocumentUnlock")
    

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

    ListBox1.Items.Add("Entering OnAfterFirstDocumentLock")
    

    listBox1.Items.Add("Entering OnAfterFirstDocumentLock");
    
  11. Save all files.

  12. Build the project and start it in debug mode by pressing F5.

    The Visual Studio experimental environment starts.

    NoteNote

    Two versions of Visual Studio are open at this point.

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

    The RDT Explorer dialog box appears and has an empty event list.

  14. In Visual Studio experimental, open or create a solution.

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

Community Additions

ADD
Show:
© 2014 Microsoft