ArcObjects Library Reference

List ArcMap Visible Toolbars Snippet

Add the visible toolbar names in an ArcMap document to a ListBox.

[C#]
/// <summary>
/// Add the visible toolbar names in an ArcMap document to a ListBox.
/// </summary>
/// <param name="application">An IApplication interface that is an IMxDocument (ArcMap).</param>
/// <param name="listbox">A System.Windows.Forms.ListBox to hold the names of the ArcMap toolbar names.</param>
/// <remarks>A CategoryFactory object is set up on the ESRI Mx Command Bars component category.
/// Note: The line of code utilizing commandBars.Find() is system resource expensive to the application as all toolbars are
/// loaded into memory and could slow down the application.</remarks>
public void ListArcMapVisibleToolbars(ESRI.ArcGIS.Framework.IApplication application, System.Windows.Forms.ListBox listbox)
{

  //Only work with ArcMap application types, not ArcCatalog, ArcGlobe, ArcToolBox, etc.
  if (! (application is ESRI.ArcGIS.ArcMapUI.IMxApplication))
  {
	return;
  }

  // Set up GUID object for 'ESRI Mx Command Bars' component category
  ESRI.ArcGIS.esriSystem.UID uid_MxCommandBars = new ESRI.ArcGIS.esriSystem.UIDClass();
  uid_MxCommandBars.Value = (System.Object)("{B56A7C4A-83D4-11d2-A2E9-080009B6F22B}"); // Explict Cast

  // Set up the category factory.
  ESRI.ArcGIS.esriSystem.ICategoryFactory categoryFactory = new ESRI.ArcGIS.esriSystem.CategoryFactoryClass();
  categoryFactory.CategoryID = uid_MxCommandBars;

  ESRI.ArcGIS.Framework.IDocument document = application.Document;

  // Go through each member of the category, and if it is a toolbar try to find it in the document
  object object_ComponentCategory = categoryFactory.CreateNext();

  while (object_ComponentCategory != null)
  {

	if (object_ComponentCategory is ESRI.ArcGIS.SystemUI.IToolBarDef)
	{

	  ESRI.ArcGIS.SystemUI.IToolBarDef toolbarDef = (ESRI.ArcGIS.SystemUI.IToolBarDef)object_ComponentCategory; //Explicit Cast
	  ESRI.ArcGIS.Framework.ICommandBars commandBars = document.CommandBars;

	  object object_ToolBarDefName = (System.Object)toolbarDef.Name; // Explict Cast
	  ESRI.ArcGIS.Framework.ICommandItem commandItem = commandBars.Find(object_ToolBarDefName, false, false);

	  ESRI.ArcGIS.Framework.ICommandBar commandBar = (ESRI.ArcGIS.Framework.ICommandBar)commandItem; //Explicit Cast

	  if (commandBar != null)
	  {

		listbox.Items.Add(toolbarDef.Caption);
		//System.Windows.Forms.MessageBox.Show(toolbarDef.Caption)

	  }

	}

	object_ComponentCategory = categoryFactory.CreateNext();

  }

}
[Visual Basic .NET]
''' <summary>
''' Add the visible toolbar names in an ArcMap document to a ListBox.
''' </summary>
''' <param name="application">An IApplication interface that is an IMxDocument (ArcMap).</param>
''' <param name="listbox">A System.Windows.Forms.ListBox to hold the names of the ArcMap toolbar names.</param>
''' <remarks>A CategoryFactory object is set up on the ESRI Mx Command Bars component category.
''' Note: The line of code utilizing commandBars.Find() is system resource expensive to the application as all toolbars are
''' loaded into memory and could slow down the application.</remarks>
Public Sub ListArcMapVisibleToolbars(ByVal application As ESRI.ArcGIS.Framework.IApplication, ByVal listbox As System.Windows.Forms.ListBox)

  'Only work with ArcMap application types, not ArcCatalog, ArcGlobe, ArcToolBox, etc.
  If Not TypeOf application Is ESRI.ArcGIS.ArcMapUI.IMxApplication Then
    Exit Sub
  End If

  ' Set up GUID object for 'ESRI Mx Command Bars' component category
  Dim uid_MxCommandBars As ESRI.ArcGIS.esriSystem.UID = New ESRI.ArcGIS.esriSystem.UIDClass
  uid_MxCommandBars.Value = CType("{B56A7C4A-83D4-11d2-A2E9-080009B6F22B}", System.Object) ' Explict Cast

  ' Set up the category factory.
  Dim categoryFactory As ESRI.ArcGIS.esriSystem.ICategoryFactory = New ESRI.ArcGIS.esriSystem.CategoryFactoryClass
  categoryFactory.CategoryID = uid_MxCommandBars

  Dim document As ESRI.ArcGIS.Framework.IDocument = application.Document

  ' Go through each member of the category, and if it is a toolbar try to find it in the document
  Dim object_ComponentCategory As System.Object = categoryFactory.CreateNext

  Do While Not object_ComponentCategory Is Nothing

    If TypeOf object_ComponentCategory Is ESRI.ArcGIS.SystemUI.IToolBarDef Then

      Dim toolbarDef As ESRI.ArcGIS.SystemUI.IToolBarDef = CType(object_ComponentCategory, ESRI.ArcGIS.SystemUI.IToolBarDef) 'Explicit Cast
      Dim commandBars As ESRI.ArcGIS.Framework.ICommandBars = document.CommandBars

      Dim object_ToolBarDefName As System.Object = CType(toolbarDef.Name, System.Object) ' Explict Cast
      Dim commandItem As ESRI.ArcGIS.Framework.ICommandItem = commandBars.Find(object_ToolBarDefName, False, False)

      Dim commandBar As ESRI.ArcGIS.Framework.ICommandBar = CType(commandItem, ESRI.ArcGIS.Framework.ICommandBar) 'Explicit Cast

      If Not commandBar Is Nothing Then

        listbox.Items.Add(toolbarDef.Caption)
        'System.Windows.Forms.MessageBox.Show(toolbarDef.Caption)

      End If

    End If

    object_ComponentCategory = categoryFactory.CreateNext

  Loop

End Sub


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