| < Free Open Study > |
|
The main file of interest that the wizard created for you is Connect.vb. As described earlier, this file contains the code for connecting and disconnecting the add-in. Although you will explore the Connect class in detail in Chapter 3, you will look at the code briefly here. You do this in order to make some minor changes and additions to the generated code; you must add some code or the add-in will not do anything but run. For example, as the wizard generated the code to create a menu and button, it did not generate any code to respond to a click of the button.
First, you look at the code the wizard generated, and then you make some minor modifications. Listing 2-1 shows what the wizard generated.
Listing 2-1: Generated Code
Imports Microsoft.Office.Core imports Extensibility Imports System.Runtime.InteropServices Imports EnvDTE #Region " Read me for Add-in installation and setup information. " ' When run, the Add-in wizard prepared the registry for the Add-in. ' At a later time, if the Add-in becomes unavailable ' for reasons such as: ' 1) You moved this project to a computer other than the one it was ' originally created on. ' 2) You chose 'Yes' when presented with a message asking if you ' wish to remove the Add-in. ' 3) Registry corruption. ' you will need to re-register the Add-in by building the ' MyAddinTest1Setup project ' by right-clicking the project in the Solution Explorer, ' then choosing install. #End Region <GuidAttribute("C4C6993D-080F-414E-982C-7846C7E5AF0E"), _ ProgIdAttribute("MyAddinTest1.Connect")> _ Public Class Connect Implements Extensibility.IDTExtensibility2 Implements IDTCommandTarget Dim applicationObject As EnvDTE.DTE Dim addInInstance As EnvDTE.AddIn Public Sub OnBeginShutdown(ByRef custom() As Object) _ Implements IDTExtensibility2.OnBeginShutdown End Sub Public Sub OnAddInsUpdate(ByRef custom() As Object) _ Implements IDTExtensibility2.OnAddInsUpdate End Sub Public Sub OnStartupComplete(ByRef custom() As Object) _ Implements IDTExtensibility2.OnStartupComplete End Sub Public Sub OnDisconnection( _ ByVal RemoveMode As ext_DisconnectMode, _ ByRef custom() As Object) _ Implements IDTExtensibility2.OnDisconnection End Sub 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) If connectMode = ext_ConnectMode.ext_cm_UISetup Then Dim objAddIn As AddIn = CType(addInInst, AddIn) Dim CommandObj As Command 'IMPORTANT! 'If your command no longer appears on the appropriate ' command bar, you add a new or modify an existing ' command, or if you would like ' to re-create the command, close all instances of Visual ' Studio .NET and double-click the file ' ReCreateCommands.reg' ' in the folder holding the source code to your Add-in. ' IMPORTANT! Try CommandObj = _ applicationObject.Commands.AddNamedCommand(objAddIn, _ "MyAddinTest1", _ "MyAddinTest1", _ "Executes the command for MyAddinTest1", _ True, 59, Nothing, 1 + 2) _ '1+2 == vsCommandStatusSupported+vsCommandStatusEnabled CommandObj.AddControl( _ applicationObject.CommandBars.Item("Tools")) Catch e As System.Exception End Try End If End Sub Public Sub Exec(ByVal cmdName As String, _ ByVal executeOption As vsCommandExecOption, _ ByRef varIn As Object, _ ByRef varOut As Object, _ ByRef handled As Boolean) _ Implements IDTCommandTarget.Exec handled = False If (executeOption = _ vsCommandExecOption.vsCommandExecOptionDoDefault) _ Then If cmdName = "MyAddinTest1.Connect.MyAddinTest1" Then handled = True Exit Sub End If End If End Sub Public Sub QueryStatus(ByVal cmdName As String, _ ByVal neededText As vsCommandStatusTextWanted, _ ByRef statusOption As vsCommandStatus, _ ByRef commandText As Object) _ Implements IDTCommandTarget.QueryStatus If neededText = _ EnvDTE.vsCommandStatusTextWanted.vsCommandStatusTextWantedNone _ Then If cmdName = "MyAddinTest1.Connect.MyAddinTest1" Then statusOption = _ CType(vsCommandStatus.vsCommandStatusEnabled + _ vsCommandStatus.vsCommandStatusSupported, _ vsCommandStatus) Else statusOption = vsCommandStatus.vsCommandStatusUnsupported End If End If End Sub End Class
The code in Listing 2-1 will run but it will not do anything but sit there. It will place a menu item with accompanying button on the Tools menu of the IDE, but again, clicking the menu item will not cause the add-in to respond.
| < Free Open Study > |
|