| < Free Open Study > |
|
At this point, let's make some minor changes to allow you to follow the progress of the add-in as it connects and disconnects. You'll cause the add-in to respond when you click its menu item. You'll add code to the OnDisconnection method, which will remove the add-in's menu item when the add-in is unloaded. Last, but certainly not least, you'll add some error-handling code to trap any error that may be encountered. I'll say this again and again throughout this book: Don't be stingy with error-handling code. Never leave anything to chance, hoping that your code won't fail. Murphy was an optimist when he said, "Anything that can fail, will fail, and it will fail at the worst time and place possible".
In order to remove the CommandObj when you disconnect, you have to change the scope of the object by moving it from the OnConnection method to the declarations section of the Connect class, as shown in Listing 2-2. This will make the object global to the class and make the object visible to the OnDisconnection method.
Listing 2-2: Moving the CommandObj to Module Level
Public Class Connect Implements IDTExtensibility2 Implements IDTCommandTarget Dim applicationObject As EnvDTE.DTE Dim addInInstance As EnvDTE.AddIn ' **** moved to module level so OnDisconnect can see it ***** Dim CommandObj As Command
In Listing 2-3, I make a few minor changes to the OnConnection method. The lines that I change appear in bold. First, I change the test for the ConnectMode argument to ext_ConnectMode.ext_cm_Startup. This will guarantee that the User Interface menu item is not added unless the add-in is really being connected for use at the time the IDE is started. Visual Studio .NET has a default option of placing an add-in's UI in the IDE and leaving it there all of the time, even when the add-in is not running. I do not recommend that you follow this method of operation. I use many add-ins from time to time, but some are used so infrequently that I certainly would not want their menus cluttering the IDE if I had no intention of using them for some period of time.
Listing 2-3: Changes to the OnConnection Method
Public Sub OnConnection(ByVal application As Object, _ ByVal connectMode As ext_ConnectMode, _ ByVal addInInst As Object, _ ByRef custom() As Object) _ Implements IDTExtensibility2.OnConnection applicationObject = CType(application, EnvDTE.DTE) addInInstance = CType(addInInst, EnvDTE.AddIn) ' changed test of connectmode from ' ext_ConnectMode.ext_cm_UISetup If connectMode = ext_ConnectMode.ext_cm_Startup Then Dim objAddIn As AddIn = CType(addInInst, AddIn) ' moved to module level so OnDisconnect can see it ' Dim CommandObj As Command MsgBox("On Connection") Try CommandObj = _ applicationObject.Commands.AddNamedCommand(objAddIn, _ "MyCommand1", _ "Click Me", _ "Executes the command for MyAddinTest1", _ True, 59, Nothing, 1 + 2) CommandObj.AddControl( _ applicationObject.CommandBars.Item("Tools")) Catch e As System.Exception MsgBox("Can't place toolbutton, error: " & _ e.Message, MsgBoxStyle.Critical, _ "MyAddinTest1") End Try End If End Sub
Next, I comment the original CommandObj Dim statement, so that you can see the point from which I moved it.
Finally, I add a couple of message boxes to trace my progress through the connection process and to inform me of any possible error encountered in placing the tool button.
Now I add the code to the OnDisconnection method to remove the CommandObj (menu item) once the add-in is unloaded. These changes appear in bold in Listing 2-4.
Listing 2-4: Changes to the OnDisconnection Method
Public Sub OnDisconnection( _ ByVal RemoveMode As ext_DisconnectMode, _ ByRef custom() As Object) _ Implements IDTExtensibility2.OnDisconnection Try ' display a message to tell what we are about to do MsgBox("Disconnect, remove Tool Button", _ MsgBoxStyle.Information, "MyAddinTest1") ' remove the add-in command button CommandObj.Delete() Catch e As System.Exception ' if we should fail to remove the button, display the error MsgBox("Error in Disconnect: " & _ e.Message, _ MsgBoxStyle.Critical, _ "MyAddinTest1") End Try End Sub
| < Free Open Study > |
|