ArcObjects Library Reference

Create Context Menu Snippet

Create a context or popup menu dynamically using some default command items.

[C#]

///<summary>Create a context or popup menu dynamically using some default command items.</summary>
///  
///<param name="application">An IApplication Interface.</param>
///   
///<remarks>Three default command items are added to the ContextMenu. Change the CLSID or ProgID as necessary for your specific command items.</remarks>
public void CreateContextMenu(ESRI.ArcGIS.Framework.IApplication application)
{
  ESRI.ArcGIS.Framework.ICommandBars commandBars = application.Document.CommandBars;
  ESRI.ArcGIS.Framework.ICommandBar commandBar = commandBars.Create("TemporaryContextMenu", ESRI.ArcGIS.SystemUI.esriCmdBarType.esriCmdBarTypeShortcutMenu);

  System.Object optionalIndex = System.Type.Missing;
  ESRI.ArcGIS.esriSystem.UID uid = new ESRI.ArcGIS.esriSystem.UIDClass();

  uid.Value = "esriArcMapUI.ZoomInFixedCommand"; // Can use CLSID or ProgID
  uid.SubType = 0;
  commandBar.Add(uid, ref optionalIndex);

  uid.Value = "{FBF8C3FB-0480-11D2-8D21-080009EE4E51}"; // Can use CLSID or ProgID
  uid.SubType = 1;
  commandBar.Add(uid, ref optionalIndex);

  uid.Value = "{FBF8C3FB-0480-11D2-8D21-080009EE4E51}"; // Can use CLSID or ProgID
  uid.SubType = 2;
  commandBar.Add(uid, ref optionalIndex);

  //Show the context menu at the current mouse location
  System.Drawing.Point currentLocation = System.Windows.Forms.Form.MousePosition;
  commandBar.Popup(currentLocation.X, currentLocation.Y);
}
[Visual Basic .NET]

'''<summary>Create a context or popup menu dynamically using some default command items.</summary>
'''  
'''<param name="application">An IApplication Interface.</param>
'''   
'''<remarks>Three default command items are added to the ContextMenu. Change the CLSID or ProgID as necessary for your specific command items.</remarks>
Public Sub CreateContextMenu(ByVal application As ESRI.ArcGIS.Framework.IApplication)

  Dim commandBars As ESRI.ArcGIS.Framework.ICommandBars = application.Document.CommandBars
  Dim commandBar As ESRI.ArcGIS.Framework.ICommandBar = commandBars.Create("TemporaryContextMenu", ESRI.ArcGIS.SystemUI.esriCmdBarType.esriCmdBarTypeShortcutMenu)

  Dim optionalIndex As System.Object = System.Type.Missing
  Dim uid As ESRI.ArcGIS.esriSystem.UID = New ESRI.ArcGIS.esriSystem.UIDClass

  uid.Value = "esriArcMapUI.ZoomInFixedCommand" ' Can use CLSID or ProgID
  uid.SubType = 0
  commandBar.Add(UID, optionalIndex)

  uid.Value = "{FBF8C3FB-0480-11D2-8D21-080009EE4E51}" ' Can use CLSID or ProgID
  uid.SubType = 1
  commandBar.Add(UID, optionalIndex)

  uid.Value = "{FBF8C3FB-0480-11D2-8D21-080009EE4E51}" ' Can use CLSID or ProgID
  uid.SubType = 2
  commandBar.Add(uid, optionalIndex)

  ' Show the context menu at the current mouse location
  Dim currentLocation As System.Drawing.Point = System.Windows.Forms.Form.MousePosition
  commandBar.Popup(currentLocation.X, currentLocation.Y)

End Sub


Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.Framework
  • ESRI.ArcGIS.System
  • ESRI.ArcGIS.SystemUI
  • System
  • System.Drawing
  • System.Windows.Forms