Adding Microsoft Office CommandBarControls

 < Free Open Study > 



Adding an Add-in DTE Toolbar

You've seen the simplest and easiest method of presenting a UI. Now let's step up a little more in complexity as well as sophistication of the UI design. In this section, you'll add a new toolbar for the add-in. Listing 7-4 shows the Connect class after it has been enhanced to add a toolbar. Code that was changed or added to create the toolbar appears in boldface.

Listing 7-4: Adding an Add-in Toolbar

start example
 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 UIMenusSetup project ' by right-clicking the project in the Solution Explorer, then choosing install. #End Region <GuidAttribute("A1DD20DF-FC66-4E5D-B0C3-1831FF589E42"), _ ProgIdAttribute("UIMenus.Connect")> _ Public Class Connect     Implements Extensibility.IDTExtensibility2     Implements IDTCommandTarget Dim oVB As EnvDTE.DTE Dim addInInstance As EnvDTE.AddIn Dim CommandObj As Command Dim CommandObj2 As Command Dim CommandObj3 As Command ' moved to module level to make visible to ' AddAddinCmdBar Dim objAddin As AddIn ' Command Bar commands and object Dim cmdObj As Command Dim cmdObj2 As Command Dim cmdbarobj As Microsoft.Office.Core.CommandBar Public Sub OnBeginShutdown(ByRef custom As System.Array) _     Implements Extensibility.IDTExtensibility2.OnBeginShutdown End Sub Public Sub OnAddInsUpdate(ByRef custom As System.Array) _     Implements Extensibility.IDTExtensibility2.OnAddInsUpdate End Sub Public Sub OnStartupComplete(ByRef custom As System.Array) _     Implements Extensibility.IDTExtensibility2.OnStartupComplete End Sub Public Sub OnDisconnection(ByVal RemoveMode As _     Extensibility.ext_DisconnectMode, _     ByRef custom As System.Array) Implements _     Extensibility.IDTExtensibility2.OnDisconnection     Try         CommandObj.Delete()         CommandObj2.Delete()         CommandObj3.Delete()         ' Delete the toolbar commands         cmdObj.Delete()         cmdObj2.Delete()         ' cmdbarobj.Delete() does not work in RC so         ' just hide the command bar         cmdbarobj.Visible = False     Catch e As System.Exception         MsgBox("Error deleting menus: " & e.Message)     End Try End Sub Public Sub OnConnection(ByVal application As Object, _     ByVal connectMode As Extensibility.ext_ConnectMode, _     ByVal addInInst As Object, _     ByRef custom As System.Array) Implements _     Extensibility.IDTExtensibility2.OnConnection     Dim iBitmap As Integer     oVB = CType(application, EnvDTE.DTE)     addInInstance = CType(addInInst, EnvDTE.AddIn)     ' test for type startup, first or subsequent     If connectMode =         Extensibility.ext_ConnectMode.ext_cm_UISetup Or          connectMode =          Extensibility.ext_ConnectMode.ext_cm_Startup Or          connectMode =          Extensibility.ext_ConnectMode.ext_cm_AfterStartup _         Then         'Dim objAddIn As AddIn = CType(addInInst, AddIn)         objAddin = CType(addInInst, AddIn)         Try             CommandObj = oVB.Commands.AddNamedCommand(objAddin, _                 "UIMenus", _                 "UIMenus One", _                 "Executes the command for UIMenus", _                 True, 59, Nothing, 1 + 2) _                 '1+2 == vsCommandStatusSupported+                 vsCommandStatusEnabled             CommandObj.AddControl(oVB.CommandBars.Item("Tools"))             CommandObj2 = oVB.Commands.AddNamedCommand(objAddin, _                 "UIMenus2", _                 "UIMenus Two", _                 "Executes the command for UIMenus", _                 True, 65, Nothing, 1 + 2) _                 '1+2 == vsCommandStatusSupported+                  vsCommandStatusEnabled                 CommandObj2.AddControl(oVB.CommandBars.Item("Tools"))             CommandObj3 = oVB.Commands.AddNamedCommand(objAddin, _                 "UIMenus3", _                 "UIMenus Three", _                 "Executes the command for UIMenus", _                 True, 73, Nothing, 1 + 2) _                 '1+2 == vsCommandStatusSupported+                  vsCommandStatusEnabled             CommandObj3.AddControl(oVB.CommandBars.Item("Tools"))             ' call method to add my toolbar             AddAddinCmdBar()         Catch e As System.Exception             MsgBox("Error Adding Menus: " & e.Message)         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 = "UIMenus.Connect.UIMenus" Then             handled = True             MsgBox("Menu one selected.")         ElseIf cmdName = "UIMenus.Connect.UIMenus2" Then             handled = True             MsgBox("Menu two selected.")         ElseIf cmdName = "UIMenus.Connect.UIMenus3" Then             handled = True             MsgBox("Menu three selected.")         ElseIf cmdName = "UIMenus.Connect.MyCommand" Then             handled = True             MsgBox("Command bar button1 clicked.")             Exit Sub         ElseIf cmdName = "UIMenus.Connect.MyCommand2" Then             handled = True             MsgBox("Command bar button2 clicked.")         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 = "UIMenus.Connect.UIMenus" Or _            cmdName = "UIMenus.Connect.UIMenus2" Or _            cmdName = "UIMenus.Connect.UIMenus3" Or _            cmdName = "UIMenus.Connect.MyCommand" Or _            cmdName = "UIMenus.Connect.MyCommand2" _            Then            statusOption = CType(vsCommandStatus.                 vsCommandStatusEnabled + _                vsCommandStatus.vsCommandStatusSupported, _                vsCommandStatus)         Else            statusOption = _                vsCommandStatus.vsCommandStatusUnsupported         End If     End If End Sub Sub AddAddinCmdBar()     ' Add a reference to the Office type library to gain     ' access to the     ' CommandBar object.     Dim cmds As Commands     Try         cmds = oVB.Commands         ' if we have already added the toolbar, set a ptr         ' to it, otherwise create the commandbar         Try             cmdbarobj = oVB.CommandBars("Mycmdbar")         Catch                 cmdbarobj = cmds.AddCommandBar("Mycmdbar", _                     vsCommandBarType.vsCommandBarTypeToolbar)             End Try             cmdbarobj.Visible = True             cmdObj = oVB.Commands.AddNamedCommand(objAddin, _                 "MyCommand", _                 "Button1", _                 "Click for Button1", _                 True, 67, Nothing, 1 + 2)             cmdObj.AddControl(cmdbarobj)             cmdObj2 = oVB.Commands.AddNamedCommand(objAddin, _                 "MyCommand2", _                 "Button2", _                 "Click for Button2", _                 True, 68, Nothing, 1 + 2)             cmdObj2.AddControl(cmdbarobj)         Catch e As System.Exception             MsgBox(e.Message)         End Try     End Sub End Class 
end example

Several changes were made to the code in Listing 7-4 to create the add-in toolbar. First, the code snippet shown in Listing 7-5 simply dimensions the new objects needed for the toolbar. Additionally, objAddin was moved to the module level to make it visible to the new AddAddinCmdBar method.

Listing 7-5: Declaring Toolbar Objects

start example
     ' AddAddinCmdBar     Dim objAddin As AddIn     ' Command Bar commands and object     Dim cmdObj As Command     Dim cmdObj2 As Command     Dim cmdbarobj As Microsoft.Office.Core.CommandBar 
end example

Finally, a whole new method named AddAddinCmdBar was added to the code. Its code is reproduced in Listing 7-6, at the bottom of the class. A call to the AddAddinCmdBar method was added to the OnConnection method.

Note 

The documentation in the MSDN help file is incorrect on the use of the AddCommandBar method. At least this is the case in the initial release version of the system. The code sequence shown in Listing 7-6 was arrived at by trial and error. Also, in the release version, the Delete method does not work. In fact, if you attempt to delete the toolbar, an error is raised.You can use the RemoveCommandBar method, which will remove the toolbar from the IDE but does not permanently delete it from Visual Studio.

Listing 7-6: AddAddinCmdBar Method

start example
 01 Sub AddAddinCmdBar() 02     'Add a reference to the Office type library to gain access to the 03     'CommandBar object. 04     Dim cmds As Commands 05     Try 06         cmds = oVB.Commands 07 08         ' if we have already added the toolbar, set a ptr 09         ' to it, otherwise create the commandbar 10         Try 11           cmdbarobj = oVB.CommandBars("Mycmdbar") 12         Catch 13           cmdbarobj = cmds.AddCommandBar("Mycmdbar", _ 14               vsCommandBarType.vsCommandBarTypeToolbar) 15         End Try 16       cmdbarobj.Visible = True 17 18       cmdObj = oVB.Commands.AddNamedCommand(objAddin, _ 19           "MyCommand", _ 20           "Button1", _ 21           "Click for Button1", _ 22           True, 67, Nothing, 1 + 2) 23       cmdObj.AddControl(cmdbarobj) 24 25       cmdObj2 = oVB.Commands.AddNamedCommand(objAddin, _ 26           "MyCommand2", _ 27           "Button2", _ 28           "Click for Button2", _ 29           True, 68, Nothing, 1 + 2) 30           cmdObj2.AddControl(cmdbarobj) 31      Catch e As System.Exception 32           MsgBox(e.Message) 33      End Try 34 End Sub 
end example

Line 11 attempts to set a CommandBar object. If Line 11 succeeds, it means that you have created the command bar in a previous execution, and you simply need to set a pointer to the existing command bar. If this line fails, it means that you have not created a command bar prior to this execution. In this case, Line 13 is executed, which creates the toolbar.

Line 13 uses the AddCommandBar method of the CommandBarControls object to create the new toolbar. The first parameter to the AddCommandBar method is the name of the new toolbar. The second parameter is the type of command bar. In this case, it is type vsCommandBarTypeToolbar.

Lines 18 through 30 simply use the methods previously described to add two buttons to the command bar.

In the OnDisconnection method in Listing 7-5, you will see that instead of deleting the command bar, you have simply set its Visible property to False to hide it. Figure 7-2 shows the new add-in toolbar that you have created. You can see that you can have picture buttons on the add-in toolbar also. Clicking either of the toolbar buttons will display a message box denoting the respective button that was clicked.

click to expand
Figure 7-2: Add-in toolbar



 < 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

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net