Displaying a TOCControl context menu


Purpose
This sample shows how context menus appear when you right-click a TOCControl that has a MapControl as its "buddy." The sample contains a map context menu and a layer context menu that host control commands and custom commands defined within the application.

How to use

See How to use ArcGIS samples for help on compiling, setting up the debugger, and running the sample. If the sample has associated data, you will find that the sample's zip file includes a "data" folder alongside the language folders. However, you will need to update the sample to point to the location of the data once you have extracted all the files.

  1. Load a map document into the MapControl.
  2. Right-click a map or layer on the TOCControl to display a context menu.

Additional information

The map context menu contains feature selection and layer visibility commands and appears on the TOCControl when a map item is clicked. The layer context menu contains selection and scale threshold commands and appears when a layer item on the TOCControl is right-clicked.
Each context menu is implemented using the ToolbarMenu coclass. In Form_Load, the AddItem method is used to add custom commands defined within the application to a map ToolbarMenu and a layer ToolbarMenu with their style set. A predefined SelectionMenu is added to the map ToolbarMenu with the AddSubMenu method. The Hook property of each ToolbarMenu is set to the MapControl. The ITOCControl.HitTest method is used in the ITOCControlEvents.OnMouseDown event when right-clicked to determine whether a map or a layer has been clicked. The ITOCControl.SelectItem method is used to select the item that is clicked. The MapControl CustomProperty is set to the resulting layer and is used by the custom commands. The PopupMenu method displays the map's ToolbarMenu or the layer's ToolbarMenu.
Several of the custom command classes, such as the LayerVisibility class, define multiple related command objects by implementing the ICommandSubType interface. For custom commands that are added to the layer's ToolbarMenu to determine which layer to work with, the IMapControl3.CustomProperty property is used.


AssemblyInfo.cs Assembly information.
TOCContextMenu.cs Form file.
LayerSelectable.cs Class file.
LayerVisibility.cs Class file.
RemoveLayer.cs Class file.
ScaleThresholds.cs Class file.
ZoomToLayer.cs Class file.
Download the C# files
AssemblyInfo.vb Assembly information.
TOCContextMenuForm.vb Form file.
LayerSelectable.vb Class file.
LayerVisibility.vb Class file.
RemoveLayer.vb Class file.
ScaleThresholds.vb Class file.
ZoomToLayer.vb Class file.
Download the VB.NET files

Download the files for all languages

See Also:

TOCControl class
ITOCControl interface
ToolbarMenu class
IToolbarMenu interface




Development licensing Deployment licensing
Engine Developer Kit Engine
ArcGIS for Desktop Basic
ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced