ArcObjects Library Reference

Add Editing Commands to ToolbarControl Snippet

Stub code for adding editing commands to a ToolbarControl.

[C#]
/// <summary>
/// Member variables for the editing context menus.
/// </summary>
/// <remarks></remarks>
public ESRI.ArcGIS.Controls.IToolbarMenu m_ToolbarMenuSketch;

/// <summary>
/// Add editing commands to a ToolbarControl.
/// </summary>
/// <param name="toolbarControl"></param>
/// <remarks>
/// Call to add the editing commands to the ToolbarControl
/// For example, if a ToolbarControl named axToolbarControl1 exists use the following code:
/// AddEditingCommandsToToolbarControl((IToolbarControl)axToolbarControl1.Object);
/// </remarks>
public void AddEditingCommandsToToolbarControl(ESRI.ArcGIS.Controls.IToolbarControl toolbarControl)
{
    // Add editing commands to ToolbarControl
    toolbarControl.AddToolbarDef("esriControls.ControlsEditingToolbar", -1, false, 0, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconOnly);

    //Create new ToolbarMenu's
    m_ToolbarMenuSketch = new ESRI.ArcGIS.Controls.ToolbarMenuClass();

    // Share the ToolbarControl's command pool
    m_ToolbarMenuSketch.CommandPool = toolbarControl.CommandPool;

    // Add commands items to the ToolbarMenu. 
    m_ToolbarMenuSketch.AddItem("esriControls.ControlsEditingSketchContextMenu", -1, -1, false, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconOnly);
}

/// <summary>
/// Popup a ToolbarMenu as a context menu.
/// </summary>
/// <param name="x">The X screen coordinate where to open the context menu.</param>
/// <param name="y">The Y screen coordinate where to open the context menu.</param>
/// <param name="hWnd">The MapControld hWnd (Window handle) toolbarControl</param>
/// <remarks>
/// Call to popup a ToolbarMenu on the display of the ToolbarControl's buddy. 
/// For example, if axToolbarControl1 is buddied to axMapControl1 use the following 
/// code from one of the MapControl's mouse events:
/// PopupToolbarMenu(e.x, e.y, axMapControl1.hWnd, (IToolbarControl)axToolbarControl1.Object);
/// </remarks>
public void PopupToolbarMenu(System.Int32 x, System.Int32 y, System.Int32 hWnd, ESRI.ArcGIS.Controls.IToolbarControl toolbarControl)
{
    ESRI.ArcGIS.SystemUI.ICommand command;
    ESRI.ArcGIS.esriSystem.UIDClass uID = new ESRI.ArcGIS.esriSystem.UIDClass();

    //Find the sketch tool in the command pool
    uID.Value = "esriControls.ControlsEditingSketchTool";
    command = toolbarControl.CommandPool.FindByUID(uID);

    //If the sketch tool is the current tool popup the sketch context menu
    if (command == toolbarControl.CurrentTool)
    {
        m_ToolbarMenuSketch.PopupMenu(x, y, hWnd);
        return;
    }
}
[Visual Basic .NET]
''' <summary>
''' Member variables for the editing context menus.
''' </summary>
''' <remarks></remarks>
Public m_ToolbarMenuSketch As ESRI.ArcGIS.Controls.IToolbarMenu

''' <summary>
''' Add editing commands to a ToolbarControl.
''' </summary>
''' <param name="toolbarControl"></param>
''' <remarks>
''' Call to add the editing commands to the ToolbarControl
''' For example, if a ToolbarControl named axToolbarControl1 exists use the following code:
''' AddEditingCommandsToToolbarControl(CType(axToolbarControl1.Object, IToolbarControl))
''' </remarks>
Public Sub AddEditingCommandsToToolbarControl(ByVal toolbarControl As ESRI.ArcGIS.Controls.IToolbarControl)

    ' Add editing commands to ToolbarControl
    toolbarControl.AddToolbarDef("esriControls.ControlsEditingToolbar", -1, False, 0, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconOnly)

    'Create new ToolbarMenu's
    m_ToolbarMenuSketch = New ESRI.ArcGIS.Controls.ToolbarMenuClass()

    ' Share the ToolbarControl's command pool
    m_ToolbarMenuSketch.CommandPool = toolbarControl.CommandPool

    ' Add commands items to the ToolbarMenu. 
    m_ToolbarMenuSketch.AddItem("esriControls.ControlsEditingSketchContextMenu", -1, -1, False, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconOnly)

End Sub

''' <summary>
''' Popup a ToolbarMenu as a context menu.
''' </summary>
''' <param name="x">The X screen coordinate where to open the context menu.</param>
''' <param name="y">The Y screen coordinate where to open the context menu.</param>
''' <param name="hWnd">The MapControld hWnd (Window handle) toolbarControl</param>
''' <remarks>
''' Call to popup a ToolbarMenu on the display of the ToolbarControl's buddy. 
''' For example, if axToolbarControl1 is buddied to axMapControl1 use the following 
''' code from one of the MapControl's mouse events:
''' PopupToolbarMenu(e.x, e.y, axMapControl1.hWnd, CType(axToolbarControl1.Object, IToolbarControl))
''' </remarks>
Public Sub PopupToolbarMenu(ByVal x As System.Int32, ByVal y As System.Int32, ByVal hWnd As System.Int32, ByVal toolbarControl As ESRI.ArcGIS.Controls.IToolbarControl)

    Dim command As ESRI.ArcGIS.SystemUI.ICommand
    Dim uID As ESRI.ArcGIS.esriSystem.UIDClass = New ESRI.ArcGIS.esriSystem.UIDClass()

    'Find the sketch tool in the command pool
    uID.Value = "esriControls.ControlsEditingSketchTool"
    command = toolbarControl.CommandPool.FindByUID(uID)

    'If the sketch tool is the current tool popup the sketch context menu
    If command Is toolbarControl.CurrentTool Then
        m_ToolbarMenuSketch.PopupMenu(x, y, hWnd)
        Return
    End If

End Sub


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