How to Add Menu Controls to a Form from an Add-In

This article was previously published under Q137038
This article has been archived. It is offered "as is" and will no longer be updated.
This article explains how to use the AddMenuTemplate method to Add menucontrols to a form from an Add-in. It also provides an exampledemonstrating how the method is used.
The FormTemplate object of the "Visual Basic Design Environment" objectmodel provides the AddMenuTemplate method. This method allows an add-in toadd menu controls to a form. It can add both top-level menus and submenus.The syntax for the AddMenuTemplate method is as follows:
   Menu = Form.AddMenuTemplate(Name, Parent)      Form   - A FormTemplate object      Name   - A string specifying the Name property for the new menu      Parent - A ControlTemplate object representing the parent menu item               that the new menu item become a child of      Menu   - A ControlTemplate object that will be set to the new menu				

To add a top-level menu to a form, you must specify a Parent object that isset to Nothing. The following shows how this is done by passing the keywordNothing as the Parent parameter:
   Set MyMenu = MyForm.AddMenuTemplate("mnuFile", Nothing)				

To add a submenu, you must specify an existing menu ControlTemplate as theParent parameter. The syntax is as follows:
   Set MySubMenu = MyForm.AddMenuTemplate("mnuFileOpen", MyMenu)				

The AddMenuTemplate method adds the new menus to the end of existing menus.Menus can't be inserted between two existing menus.

Once you have added a menu, you can modify its properties to:
  • Change the caption
  • Add a shortcut key
  • Make a menu separator
  • Enable it
  • Hide it
  • Turn it into a control array
  • And more
The object returned by the AddMenuTemplate method is a ControlTemplateobject that references the newly added menu. The properties of the menu canbe set through the Properties collection of the ControlTemplate. Forexample, to change the caption, use this:
   MyMenu.Properties("&Caption").Value = "New Caption"				

You can add an access key to any menu by placing an ampersand (&) in frontof the desired access key in the caption. In the preceding example, C wouldbe the access key.

You can only add a shortcut key to a submenu. To set a shortcut key, yougain access to the Shortcut property in the same manner that you gainaccess to the Caption property. The Shortcut property is an enumeratedproperty. Visual Basic defines constants such as vbMenuAccelCtrlO forsetting the Shortcut property.

To create a menu separator, first add a submenu. Next, set the Captionproperty to a dash. This indicates to Visual Basic that you want a menuseparator instead of a submenu.

To hide a menu item, set the Visible property to False. This will notwork on a submenu if it is the only visible submenu.

Step-by-Step Example

  1. Start a new project in Visual Basic.
  2. Remove the default form (Form1).
  3. Add a new standard module (Module1.bas by default.)
  4. Put the following code in the standard module:
       ''' MODULE1.BAS   Option Explicit   #If Win16 Then      Declare Function WritePrivateProfileString Lib "KERNEL" ( _         ByVal AppName As String, ByVal KeyName As String, _         ByVal keydefault As String, ByVal FileName As String) As Integer      Declare Function GetPrivateProfileString Lib "KERNEL" ( _         ByVal AppName As String, ByVal KeyName As String, _         ByVal keydefault As String, ByVal ReturnString As String, _         ByVal NumBytes As Integer, ByVal FileName As String) As Integer   #ElseIf Win32 Then      Declare Function WritePrivateProfileString Lib "Kernel32" _         Alias "WritePrivateProfileStringA" (ByVal AppName As String, _         ByVal KeyName As String, ByVal keydefault As String, _         ByVal FileName As String) As Long      Declare Function GetPrivateProfileString Lib "Kernel32" _         Alias "GetPrivateProfileStringA" (ByVal AppName As String, _         ByVal KeyName As String, ByVal keydefault As String, _         ByVal ReturnString As String, ByVal NumBytes As Long, _         ByVal FileName As String) As Long   #End If   Sub Main()      #If Win16 Then         Const Section = "Add-Ins16"      #ElseIf Win32 Then         Const Section = "Add-Ins32"      #End If      Const BufSize = 255      Dim Ret As Variant      Dim RetStr As String      ' Hide the Addin from the Task Manager      App.TaskVisible = False      ' Check to see if the entry is already in the VB.ini file.      ' Add if not.      RetStr = Space(BufSize)      Ret = GetPrivateProfileString(Section, "AddinExample.MenuControl", _         "NotFound", RetStr, BufSize, "VB.INI")      RetStr = Left(RetStr, Ret)      If RetStr = "NotFound" Then         WritePrivateProfileString Section, "AddinExample.MenuControl", _            "0", "VB.INI"      End If   End Sub						
  5. Add a new class module to the project (Class1.cls by default.)
  6. Set the following properties of the class module to the specified values:
       Property        Value   ---------------------------------------   Instancing      1 - Creatable SingleUse   Name            MenuControl   Public          True						
  7. Put the following code in the class module:
       ''' CLASS1.CLS   Option Explicit   Private ThisInstance As Object 'VBIDE.Application   Private AddInMenuLine As Object 'VBIDE.SubMenu   Private AddInID As Long   Public Sub ConnectAddIn(VBInstance As Object)      Set ThisInstance = VBInstance      Set AddInMenuLine = ThisInstance.AddInMenu.MenuItems.Add( _         "Menu Control Example")      AddInID = AddInMenuLine.ConnectEvents(Me)   End Sub   Public Sub DisconnectAddIn(Mode As Integer)      AddInMenuLine.DisconnectEvents AddInID      ThisInstance.AddInMenu.MenuItems.Remove AddInMenuLine   End Sub   Public Sub AfterClick()      Dim CurrentForm As Object 'VBIDE.FormTemplate      Dim mnuFile As Object 'VBIDE.ControlTemplate      Dim mnuFileOpen As Object 'VBIDE.ControlTemplate      Dim mnuFileSeparator As Object 'VBIDE.ControlTemplate      Dim mnuFileMRU As Object 'VBIDE.ControlTemplate      ' Set a reference to the Active Form      Set CurrentForm = ThisInstance.ActiveProject.ActiveForm      ' Create the top-level File menu      Set mnuFile = CurrentForm.AddMenuTemplate("mnuFile", Nothing)      mnuFile.Properties("Caption").Value = "&File"      ' Add the Open menu under the File menu      Set mnuFileOpen = CurrentForm.AddMenuTemplate("mnuFileOpen", mnuFile)      mnuFileOpen.Properties("Caption").Value = "&Open"      mnuFileOpen.Properties("Shortcut").Value = vbMenuAccelCtrlO      ' Add a separator      Set mnuFileSeparator = CurrentForm.AddMenuTemplate _         ("mnuFileSeparator", mnuFile)      mnuFileSeparator.Properties("Caption").Value = "-"      ' Add MRU under the File menu      Set mnuFileMRU = CurrentForm.AddMenuTemplate("mnuFileMRU", mnuFile)      mnuFileMRU.Properties("Caption").Value = "MRU"      mnuFileMRU.Properties("Visible").Value = False ' Make Invisible      mnuFileMRU.Properties("Index").Value = 0  ' Make into control array   End Sub						
  8. Set the following Project Options:
       Option            Value   ---------------------------------------   Startup Form      Sub Main   Project Name      AddinExample   StartMode         OLE Server   Error Trapping    Break in Class Module						
  9. Save the project.
  10. Run the project.
  11. Start a second instance of Visual Basic. Form1 is created by default.
  12. Use the Add-In Manager to add the AddinExample.MenuControl Add-In.
  13. Set the focus to Form1.
  14. On the Add-In menu, click Menu Control Example.
The Add-In should add a File menu to Form1. Under the file menu will be anOpen submenu. The Open submenu will have CTRL+O as a shortcut key. Belowthe Open submenu will be a menu separator. Below the separator there willbe an invisible MRU menu control array.
4.00 vb4win vb4all

Article ID: 137038 - Last Review: 02/28/2014 07:58:36 - Revision: 2.0

Microsoft Visual Basic 4.0 Standard Edition, Microsoft Visual Basic 4.0 Professional Edition, Microsoft Visual Basic 4.0 Professional Edition, Microsoft Visual Basic 4.0 16-bit Enterprise Edition, Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

  • kbnosurvey kbarchive KB137038