[GIS] Add and remove tools from ArcMap toolbar via C# Button and ArcObjects

arcgis-10.2arcobjectsc

I am trying to create a custom button in arcmap that when pressed will edit a toolbars content. Doing things like adding or removing tools from a toolbar. An example would be to create a button that when pressed removes or toggles all the text/font editing tools from the draw toolbar.

I've been Looking at with IToolbarControl2
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/IToolbarControl2_Interface/0016000003m7000000/
It seems to give access to various member functions that let me edit what I want. unfortunately I'm unsure of how to reference the specific toolbar that I want to edit.

I made some edits and currently the code runs and then hangs when i attempt to define axToolbarControl1.AddToolbarDef via:

axToolbarControl1.AddToolbarDef(uID, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);

The current draft of the code is the following:

///Code to add or remove tools from a toolbar using System;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.ArcMap;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS;

namespace RemoveAllTools2
{
public class RemoveAllTools2 : ESRI.ArcGIS.Desktop.AddIns.Button
{
    IApplication m_application;        //ArcMap application
    IMxDocument m_mxDocument;          //ArcMap document
    public ESRI.ArcGIS.Controls.ToolbarControl axToolbarControl1;
    public UID uID = new UIDClass();

    public RemoveAllTools2()
    {
        MessageBox.Show("setup start");
        m_application = ArcMap.Application;
        MessageBox.Show("m_application set");
        UID uID = new UIDClass();
        uID.Value = "esriArcMapUI.DrawToolBar";
        MessageBox.Show("UID set");

        //code hangs here
        axToolbarControl1.AddToolbarDef(uID, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);
        //code hangs here

        MessageBox.Show("axtoolbar set");

    }

    protected override void OnClick()
    {
        MessageBox.Show("clicked");
        IToolbarControl2 ttoolbarControl = axToolbarControl1.Object as IToolbarControl2;
        MessageBox.Show("toolbarControl set");
        uID.Value = "esriArcMapUI.FontNameControl";

        int toolFindIndex = ttoolbarControl.Find(uID);
        if (toolFindIndex!=-1)
        {
             ttoolbarControl.Remove(toolFindIndex);
             MessageBox.Show("Tool remove run finished "); 
        }
        else
        {
            MessageBox.Show("Tool not found");
        }
        MessageBox.Show("IF run finished ");   

    }
    protected override void OnUpdate()
    {

    }
}

}

Best Answer

ESRI.ArcGIS.Controls.ToolbarControl class is for ArcGIS Engine Application and you shouldn't use it in ArcMap with ArcObject.

To Remove a command from a toolbar: You just need the name or UID of the command you want to remove from a toolbar. Suppose you want to delete fullExtent command from the Tools toolbar:

var fullExtent = asraArcMapEngine.getCommandItem("esriArcMapUI.FullExtentCommand");
fullExtent.Delete();

To Add a button to a Toolbar: 1- first you need to get a handle of the toolbar using this method:

public static ICommandBar GetToolbarByName(IApplication mapplication,String toolbarName)// Example: "esriArcMapUI.StandardToolBar"
            {
                ESRI.ArcGIS.Framework.ICommandBars commandBars = mapplication.Document.CommandBars;
                ESRI.ArcGIS.esriSystem.UID barID = new ESRI.ArcGIS.esriSystem.UIDClass();
                barID.Value = toolbarName; 
                ESRI.ArcGIS.Framework.ICommandItem commandItem = commandBars.Find(barID, false, false);
                if (commandItem != null && commandItem.Type == ESRI.ArcGIS.Framework.esriCommandTypes.esriCmdTypeToolbar)
                {
                    return (ESRI.ArcGIS.Framework.ICommandBar)commandItem;
                }
                else
                    return null;
            }

Then use the method of ICommandBar to add your commands:

ICommandBar cmdBar = GetToolbarByName(application,"esriArcMapUI.StandardToolBar")
cmdBar.Add("UID_of_MyCustomCommand");
Related Question