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.
Create the tool window and connect it to the RDT events.
To subscribe to RDT events
-
On the File menu, point to New, and then click Project.
-
In the New Project dialog box, expand Other Project Types, and then click Extensibility Projects.
-
In the Templates pane click Visual Studio Integration Package.
-
In the Location box, type the file path for your VSPackage.
-
In the Name box, type RDTExplorer for the solution and then click OK to start the wizard.
-
In the Select a Programming Language page, select Visual C#.
-
In the Select VSPackage Options page, select Tool Window.
-
In 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.
-
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.
-
Open the file, RDTExplorer/MyControl.cs, in code view. Add these lines to the using section at the start of the file:
using Microsoft.VisualStudio; using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell.Interop;
-
Modify the MyControl class so that it derives from both UserControl and IVsRunningDocTableEvents.
public class MyControl : System.Windows.Forms.UserControl, IVsRunningDocTableEvents
-
Click the word, IVsRunningDocTableEvents.
A SmartTag appears as a narrow rectangular box under the letter "I".
-
Pause the move pointer over the SmartTag.
A drop-down list appears.
-
Select Explicitly implement interface "IVsRunningDocTableEvents" from the drop-down list.
The SmartTag writes a simple implementation of this interface.
-
Replace the body of each method in the interface with this code:
return VSConstants.S_OK;
-
Add a cookie holder to the MyControl class:
public partial class MyControl : UserControl, IVsRunningDocTableEvents private uint dwCookie;
This variable holds the cookie returned by AdviseRunningDocTableEvents until the MyControl.Dispose method is called.
-
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 implementing IVsRunningDocTableEvents, in this case, the MyControl instance.
-
Open the file MyControlDesigner.cs and add the following using statements just after the namespace directive:
namespace Company.RDTExplorer { using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell.Interop; -
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.
-
In the MyControl.cs file, add this line to the body of the OnBeforeLastDocumentUnlock handler, just before the return statement:
listBox1.Items.Add("Entering OnBeforeLastDocumentUnlock"); -
Add a similar line to the body of the OnAfterFirstDocumentLock handler and to other events you want to see in the ListBox:
listBox1.Items.Add("Entering OnAfterFirstDocumentLock"); -
Build and start the project in debug mode by pressing the keyboard shortcut, F5.
Visual Studio Exp starts.
Note Both versions of Visual Studio are open at this point.
-
On the View menu of 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.
-
In Visual Studio Exp, open or create a solution.
As OnBeforeLastDocument and OnAfterFirstDocument events are fired, notification of each event appears in listBox1.