HOW TO

Toggle the visibility for a LYR or group layer legend using ArcObjects

Last Published: April 25, 2020

Summary

Note:
This article pertains to ArcGIS versions 8.x and 9.x. Later versions of ArcGIS may contain different functionality, as well as different names and locations for menus, commands and geoprocessing tools.

Instructions provide a code sample that shows how to toggle the visibility of the Table of Contents legend for a group layer or LYR file.

Note: 
Support for Visual Basic for Applications (VBA) for ArcMap and ArcCatalog ended with the ArcGIS 10.2.2 release, and Esri has not included VBA compatibility setups since version 10.5. See: ArcGIS Desktop and VBA Moving Forward

Procedure

  1. Start ArcMap.
  2. Create a new UIButtonControl: How To: Create a new UIButtonControl​
Note:
For more information on creating a UIControl, see the ArcGIS Desktop Help topic: 'Creating custom commands with VBA and UI Controls'
  1. Right-click the UIButtonControl and select View Source.
  2. Copy this code into the UIButtonControl's click event.
Dim pMxDoc As IMxDocument
Dim pGroupLayer As IGroupLayer
Dim pLayer As ILayer
Dim pLegendInfo As ILegendInfo
Dim pLegendGroup As ILegendGroup

Set pMxDoc = ThisDocument
Set pLayer = pMxDoc.SelectedLayer

If Not TypeOf pLayer Is IGraphicsLayer Then
If TypeOf pLayer Is IGroupLayer Then
    Set pGroupLayer = pLayer
    pGroupLayer.Expanded = Not pGroupLayer.Expanded
Else
    Set pLegendInfo = pLayer
    Set pLegendGroup = pLegendInfo.LegendGroup(0)
    pLegendGroup.Visible = Not pLegendGroup.Visible
End If
    pMxDoc.ActiveView.Refresh
    pMxDoc.UpdateContents
End If
  1. Select a layer file in the Table of Contents and press the button to test the code sample.

Article ID:000006267

Software:
  • ArcMap 8 x
  • ArcMap 9 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic