ArcObjects Library Reference

Toggle Visibility Of Composite Layer Snippet

Toggle the visibility on and off for a composite layer in the TOC of a map.

[C#]
/// <summary>
/// Toggle the visibility on and off for a composite layer in the TOC of a map.
/// </summary>
/// <param name="activeView">An IActiveView interface.</param>
/// <param name="layerIndex">A Int32 that is the index number of the composite layer in the TOC. Example: 0.</param>
/// <remarks>This snippet is useful for toggling the visibility of composite layers. An example of a composite layer is feature linked annotation.</remarks>
public void ToggleVisibleOfCompositeLayer(ESRI.ArcGIS.Carto.IActiveView activeView, System.Int32 layerIndex)
{

    ESRI.ArcGIS.Carto.IMap map = activeView.FocusMap;
    ESRI.ArcGIS.Carto.ILayer layer = map.get_Layer(layerIndex);
    ESRI.ArcGIS.Carto.ICompositeLayer2 compositeLayer2 = (ESRI.ArcGIS.Carto.ICompositeLayer2)layer;
    System.Int32 compositeLayerIndex = 0;

    if (layer.Visible)
    {

        //Turn the layer visibility off
        layer.Visible = false;

        //Turn each sub-layer (ie. composite layer) visibility off
        for (compositeLayerIndex = 0; compositeLayerIndex < compositeLayer2.Count; compositeLayerIndex++)
        {
            compositeLayer2.get_Layer(compositeLayerIndex).Visible = false;
        }

    }
    else
    {

        //Turn the layer visibility on
        layer.Visible = true;

        //Turn each sub-layer (ie. composite layer) visibility on
        for (compositeLayerIndex = 0; compositeLayerIndex < compositeLayer2.Count; compositeLayerIndex++)
        {
            compositeLayer2.get_Layer(compositeLayerIndex).Visible = true;
        }

    }

    //Refresh the TOC and the map window
    activeView.ContentsChanged();

}
[Visual Basic .NET]
''' <summary>
''' Toggle the visibility on and off for a composite layer in the TOC of map.
''' </summary>
''' <param name="activeView">An IActiveView interface.</param>
''' <param name="layerIndex">A Int32 that is the index number of the composite layer in the TOC. Example: 0.</param>
''' <remarks>This snippet is useful for toggling the visibility of composite layers. An example of a composite layer is feature linked annotation.</remarks>
Public Sub ToggleVisibilityOfCompositeLayer(ByVal activeView As ESRI.ArcGIS.Carto.IActiveView, ByVal layerIndex As System.Int32)

  Dim map As ESRI.ArcGIS.Carto.IMap = activeView.FocusMap
  Dim layer As ESRI.ArcGIS.Carto.ILayer = map.Layer(layerIndex)
  Dim compositeLayer2 As ESRI.ArcGIS.Carto.ICompositeLayer2 = CType(layer, ESRI.ArcGIS.Carto.ICompositeLayer2)
  Dim compositeLayerIndex As System.Int32 = 0

  If layer.Visible Then

    'Turn the layer visibility off
    layer.Visible = False

    'Turn each sub-layer (ie. composite layer) visibility off
    For compositeLayerIndex = 0 To compositeLayer2.Count - 1
      compositeLayer2.Layer(compositeLayerIndex).Visible = False
    Next

  Else

    'Turn the layer visibility on
    layer.Visible = True

    'Turn each sub-layer (ie. composite layer) visibility on
    For compositeLayerIndex = 0 To compositeLayer2.Count - 1
      compositeLayer2.Layer(compositeLayerIndex).Visible = True
    Next

  End If

  'Refresh the TOC and the map window
  activeView.ContentsChanged()

End Sub


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