This document is archived and information here might be outdated.  Recommended version.


Get Command on Toolbar by Names Snippet (ArcObjects .NET 10.5 SDK)
ArcObjects Library Reference

Get Command on Toolbar by Names Snippet

Find a command item particularly on a toolbar.

[C#]
///<summary>Find a command item particularly on a toolbar.</summary>
///  
///<param name="application">An IApplication interface.</param>
///<param name="toolbarName">A System.String that is the name of the toolbar to return. Example: "esriArcMapUI.StandardToolBar"</param>
///<param name="commandName">A System.String that is the name of the command to return. Example: "esriFramework.HelpContentsCommand"</param>
///   
///<returns>An ICommandItem interface.</returns>
///  
///<remarks>Refer to the EDN document http://edndoc.esri.com/arcobjects/9.1/default.asp?URL=/arcobjects/9.1/ArcGISDevHelp/TechnicalDocuments/Guids/ArcMapIds.htm for a listing of available CLSID's and ProgID's that can be used as the toolbarName and commandName parameters.</remarks>
public ESRI.ArcGIS.Framework.ICommandItem GetCommandOnToolbar(ESRI.ArcGIS.Framework.IApplication application, System.String toolbarName, System.String commandName)
{
  ESRI.ArcGIS.Framework.ICommandBars commandBars=application.Document.CommandBars;
  ESRI.ArcGIS.esriSystem.UID barID=new ESRI.ArcGIS.esriSystem.UIDClass();
  barID.Value=toolbarName; // Example: "esriArcMapUI.StandardToolBar"
  ESRI.ArcGIS.Framework.ICommandItem barItem=commandBars.Find(barID, false, false);

  if (barItem != null && barItem.Type == ESRI.ArcGIS.Framework.esriCommandTypes.esriCmdTypeToolbar)
  {
    ESRI.ArcGIS.Framework.ICommandBar commandBar=(ESRI.ArcGIS.Framework.ICommandBar)barItem;
    ESRI.ArcGIS.esriSystem.UID commandID=new ESRI.ArcGIS.esriSystem.UIDClass();
    commandID.Value=commandName; // Example: "esriArcMapUI.AddDataCommand"
    return commandBar.Find(commandID, false);
  }
  else
    return null;
}
[Visual Basic .NET]
'''<summary>Find a command item particularly on a toolbar.</summary>
'''  
'''<param name="application">An IApplication interface.</param>
'''<param name="toolbarName">A System.String that is the name of the toolbar to return. Example: "esriArcMapUI.StandardToolBar"</param>
'''<param name="commandName">A System.String that is the name of the command to return. Example: "esriFramework.HelpContentsCommand"</param>
'''   
'''<returns>An ICommandItem interface.</returns>
'''  
'''<remarks>Refer to the EDN document http://edndoc.esri.com/arcobjects/9.1/default.asp?URL=/arcobjects/9.1/ArcGISDevHelp/TechnicalDocuments/Guids/ArcMapIds.htm for a listing of available CLSID's and ProgID's that can be used as the toolbarName and commandName parameters.</remarks>
Public Function GetCommandOnToolbar(ByVal application As ESRI.ArcGIS.Framework.IApplication, ByVal toolbarName As System.String, ByVal commandName As System.String) As ESRI.ArcGIS.Framework.ICommandItem

  Dim commandBars As ESRI.ArcGIS.Framework.ICommandBars=application.Document.CommandBars
  Dim barID As ESRI.ArcGIS.esriSystem.UID=New ESRI.ArcGIS.esriSystem.UIDClass
  barID.Value=toolbarName ' Example: "esriArcMapUI.StandardToolBar"
  Dim barItem As ESRI.ArcGIS.Framework.ICommandItem=commandBars.Find(barID, False, False)

  If Not (barItem Is Nothing) AndAlso barItem.Type=ESRI.ArcGIS.Framework.esriCommandTypes.esriCmdTypeToolbar Then

    Dim commandBar As ESRI.ArcGIS.Framework.ICommandBar=CType(barItem, ESRI.ArcGIS.Framework.ICommandBar)
    Dim commandID As ESRI.ArcGIS.esriSystem.UID=New ESRI.ArcGIS.esriSystem.UIDClass
    commandID.Value=commandName ' Example: "esriArcMapUI.AddDataCommand"
    Return commandBar.Find(commandID, False)

  Else

    Return Nothing

  End If

End Function

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