KnowDotNet

How to Add Nested Menus To an Add-In in VS2005

What's New In VS2005 Extensibility?

by Les Smith

How do I create menus and sub menus in VS2005?  Microsoft Office CommandBars don't appear to work.  You're right; VS2005 (Whidbey) has its own CommandBars so that you don't incur the overhead of carrying Office.dll around with your add-ins.

This article will show you how to create a context menu in the Code Window and add sub menus to it.  For this article, I used the Add-In Wizard to create the basic Add-In, but I did not let it create a menu for me.  I will create all the menus myself.  I need to add one Namespace Import to the imports which will make them look like the following.  The Namespace that I add is for the CommandBars.  Note that it is now built into VisualStudio so that we don't need a reference to Microsoft.Office.

Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80

Next comes the class variables for the menu items and the application object, which I have shortened from applicationObject to oVS since I will be using it many times in the add-in.  Following that will come the CommandBar objects from which I will create the menus.

    ' object for the Code Window menu
    Private cmdbar As Microsoft.VisualStudio.CommandBars.CommandBar
    
' two popup menus, 1 for the code window
    Private popup As CommandBarPopup
    
' 1 for a sub menu which will contain two other sub menus
    Private popup2 As CommandBarPopup
    
' 3 sub menus, two will go on a sub menu and 1 on my main popup
    Private WithEvents mnuAbout As CommandBarButton
    
Private WithEvents mnuSetup As CommandBarButton
    
Private WithEvents mnuTest As CommandBarButton

In the OnConnection method I place a call to the CreateMenus method to create my menus.

    Public Sub OnConnection(ByVal application As Object, _
        
ByVal connectMode As ext_ConnectMode, ByVal addInInst As Object, _
        
ByRef custom As Array) Implements IDTExtensibility2.OnConnection
        
Try
            oVS = CType(application, DTE2)
            _addInInstance =
CType(addInInst, AddIn)
            CreateMenus()
        
Catch ex As Exception
            MsgBox(ex.ToString)
        
End Try
    End Sub

The CreateMenus method creates the menus.

   Private Sub CreateMenus()
      
Try
         Dim nullParam As Object = Type.Missing
        
' create a popup menu on the Code Window popup menu
         cmdbar = CType(CType(oVS.CommandBars, CommandBars)("Code Window"), _
            CommandBar)
         popup =
CType(cmdbar.Controls.Add(MsoControlType.msoControlPopup, _
            nullParam, nullParam, 2, nullParam), CommandBarPopup)
         popup.Caption =
"Net Completer/VB"

         ' add another popup to my main popup
         popup2 = CType(popup.Controls.Add(MsoControlType.msoControlPopup, _
            nullParam, nullParam, 1, nullParam), CommandBarPopup)
         popup2.Caption =
"Misc"

         ' add two sub menus to the popup just created
         mnuAbout = CType(popup2.Controls.Add(MsoControlType.msoControlButton), _
            CommandBarButton)
         mnuAbout.Caption =
"About"
         mnuAbout.FaceId = 202
         mnuSetup =
CType(popup2.Controls.Add(MsoControlType.msoControlButton), _
            CommandBarButton)
         mnuSetup.Caption =
"Setup"
         mnuSetup.FaceId = 203
        
' add a menu to the main menu of my addin
         mnuTest = CType(popup.Controls.Add(MsoControlType.msoControlButton), _
            CommandBarButton)
         mnuTest.Caption =
"Test"
         mnuTest.FaceId = 204

      
Catch ex As Exception
         MsgBox(ex.ToString())
      
End Try

Finally, I need to add event handlers for the three sub menus.

   Private Sub mnuAbout_Click(ByVal Ctrl As _
     Microsoft.VisualStudio.CommandBars.CommandBarButton, _
    
ByRef CancelDefault As Boolean) Handles mnuAbout.Click
      MsgBox(
"About")
  
End Sub

   Private Sub mnuSetup_Click(ByVal Ctrl As _
      Microsoft.VisualStudio.CommandBars.CommandBarButton, _
      
ByRef CancelDefault As Boolean) Handles mnuSetup.Click
      MsgBox(
"Setup")
  
End Sub

   Private Sub mnuTest_Click(ByVal Ctrl As _
      Microsoft.VisualStudio.CommandBars.CommandBarButton, _
      
ByRef CancelDefault As Boolean) Handles mnuTest.Click
      MsgBox(
"Test Menu")
  
End Sub

That's all there is to it.  Thanks to Ognjen Bajic for help in his blog.