< Free Open Study > 

Event Handling

There is one last area that I want to cover with this add-in. In Chapter 11, I covered the use of events. In a couple of those events, I inserted some useful functionality. The first was a reminder to notify you when you have failed to change the default name of a form, class, or module. The second was to cause the Task List to open automatically when an error item is added to the Task List. I have added these two events to the Connect class. In the declaration section of the class, I have placed the following code snippet. This code declares the events for the two items mentioned previously.

 ' declare DTE events we want to handle Public WithEvents eventWindows As EnvDTE.WindowEvents Public WithEvents taskListEvents As EnvDTE.TaskListEvents 

In the OnConnection method of the class, I have placed the code shown in Listing 12-15, which will associate the events with their handlers. This code will also create an instance of the CReminders class that will be called by the event handlers to process the information from the events. If you need a more thorough explanation of what happens in these events, please refer back to Chapter 11.

Listing 12-15: Linking the Events to Event Handlers

start example
 ' link DTE events we want to handle Dim events As EnvDTE.Events events = oVB.Events eventWindows = CType(events.WindowEvents(Nothing), _    EnvDTE.WindowEvents) taskListEvents = CType(events.TaskListEvents(Nothing), _    EnvDTE.WindowEvents) oRemind = New CReminders(oVB) 
end example

Listing 12-16 shows the code for the event handlers that are linked to the events by the code in Listing 12-15.

Listing 12-16: Task List and Windows Event Handlers

start example
 ' Event handlers for DTE events we are handling Private Sub eventWindows_WindowActivated(ByVal _    GotFocus As EnvDTE.Window, _    ByVal LostFocus As EnvDTE.Window) _    Handles eventWindows.WindowActivated    ' Let's do something useful    ' if the window name is a default name, e.g, Form(n),    ' Module(n) or Class(n)    ' suggest to the user that they need to rename it    oRemind.CkForRemindOfDefaultName(GotFocus.Caption)    ' task item removed does not always fire because of    ' multiple events firing, so place the calls in this    ' event to kludge the closing of the task list    If GotFocus.Caption.StartsWith("Task List") Then       oRemind.CkForClosingTaskList()    End If    If LostFocus.Caption.StartsWith("Task List") Then       oRemind.CkForClosingTaskList()    End If End Sub Private Sub taskListEvents_TaskAdded(ByVal _    TaskItem As EnvDTE.TaskItem) _    Handles taskListEvents.TaskAdded    ' activate the task window    oRemind.ActivateTaskList() End Sub 
end example

Figure 12-14 shows what happens when a project opens and a form still having a default name comes up in the active window. The developer is immediately reminded that the form's name should be changed. The developer will continue to be reminded once per instance of the IDE to do so until he or she changes the name.

click to expand
Figure 12-14: Default form name detected

 < Free Open Study > 

Writing Add-Ins for Visual Studio  .NET
Writing Add-Ins for Visual Studio .NET
ISBN: 1590590260
EAN: 2147483647
Year: 2002
Pages: 172
Authors: Les Smith © 2008-2017.
If you may any questions please contact us: