Implementing a property page for an ArcGIS Engine application
TocContextMenuClass.vb
' Copyright 2012 ESRI
' 
' All rights reserved under the copyright laws of the United States
' and applicable international laws, treaties, and conventions.
' 
' You may freely redistribute and use this sample code, with or
' without modification, provided you include the original copyright
' notice and use restrictions.
' 
' See the use restrictions.
' 

Imports Microsoft.VisualBasic
Imports System
Imports System.Runtime.InteropServices
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.SystemUI
Imports ESRI.ArcGIS.Controls

Namespace MapControlAppPropertyPage

  ''' <summary>
  ''' Context menu class hosting ArcObject commands
  ''' </summary>
  Public Class TocContextMenuClass
  'class members
  'the underlying toolbarMenu that will be used
  Protected m_toolbarMenu As IToolbarMenu2 = Nothing

  'a data-structure used in order to store and manage item definitions
  Private Structure ItemDef
    'public members
    Public itemDef As String
    Public group As Boolean
    Public subType As Integer

    'constructor
      Public Sub New(ByVal itd As String, ByVal grp As Boolean, ByVal subtype As Integer)
        itemDef = itd
        group = grp
        subtype = subtype
      End Sub
  End Structure

  'array of item definitions with will be used to create the commends for the context menu
  Private m_itemDefs As ItemDef() = { New ItemDef("SymbolSelector.FeatureLayerSymbology", False, -1) }


  ''' <summary>
  ''' class constructor
  ''' </summary>
  ''' <param name="hook"></param>
  Public Sub New(ByVal hook As Object)
    m_toolbarMenu = New ToolbarMenuClass()
    m_toolbarMenu.SetHook(hook)

    AddItems()

  End Sub

  ''' <summary>
  ''' popup the context menu at the given location
  ''' </summary>
  ''' <param name="X"></param>
  ''' <param name="Y"></param>
  ''' <param name="hWndParent"></param>
  Public Sub PopupMenu(ByVal X As Integer, ByVal Y As Integer, ByVal hWndParent As Integer)
    m_toolbarMenu.PopupMenu(X, Y, hWndParent)
  End Sub

  ''' <summary>
  ''' add the items from the ItemDef array to the menu
  ''' </summary>
  Private Sub AddItems()
    Try
    Dim obj As Object = Nothing
    For Each item As ItemDef In m_itemDefs
      Try
      obj = Activator.CreateInstance(Type.GetTypeFromProgID(item.itemDef))
      Catch
      Continue For
      End Try

      m_toolbarMenu.AddItem(obj, item.subType, -1, item.group, esriCommandStyles.esriCommandStyleIconAndText)
    Next item
    Catch ex As Exception
    System.Diagnostics.Trace.WriteLine(ex.Message)
    End Try
  End Sub


  ''' <summary>
  ''' Incase that the user would want to add an item at runtime
  ''' </summary>
  ''' <param name="item"></param>
  ''' <param name="beginGroup"></param>
  ''' <param name="subType"></param>
  Public Sub AddItem(ByVal item As Object, ByVal beginGroup As Boolean, ByVal subType As Integer)
    m_toolbarMenu.AddItem(item, subType, -1, beginGroup, esriCommandStyles.esriCommandStyleIconAndText)
  End Sub
  End Class
End Namespace