Zoom to Layer by Index Number Snippet (ArcObjects .NET 10.6 SDK)
ArcObjects Library Reference

Zoom to Layer by Index Number Snippet

Zooms to the specified layer in the active view at a specific index number.

[C#]
///<summary>Zooms to the specified layer in the active view at a specific index number.</summary>
/// 
///<param name="activeView">An IActiveView interface</param>
///<param name="layerIndex">A System.Int32 that is the index number of the layer to zoom to. Example: 0</param>
///  
///<remarks>Index number 0 is the first layer in the Active View.</remarks>
public void ZoomToLayerByIndexNumber(ESRI.ArcGIS.Carto.IActiveView activeView, System.Int32 layerIndex)
{

  if (activeView == null || layerIndex < 0)
  {
    return;
  }
  ESRI.ArcGIS.Carto.IMap map=activeView.FocusMap;

  // Ensure the layer is available(layerIndex is 0-based)
  if (layerIndex >= map.LayerCount)
  {
    return;
  }

  // Refresh and Zoom to the layer
  activeView.Extent=map.get_Layer(layerIndex).AreaOfInterest;
  activeView.Refresh();
}
[Visual Basic .NET]
'''<summary>Zooms to the specified layer in the active view at a specific index number.</summary>
''' 
'''<param name="activeView">An IActiveView interface</param>
'''<param name="layerIndex">A System.Int32 that is the index number of the layer to zoom to. Example: 0</param>
'''  
'''<remarks>Index number 0 is the first layer in the Active View.</remarks>
Public Sub ZoomToLayerByIndexNumber(ByVal activeView As ESRI.ArcGIS.Carto.IActiveView, ByVal layerIndex As System.Int32)

  If activeView Is Nothing OrElse layerIndex < 0 Then
        Return
  End If

  Dim map As ESRI.ArcGIS.Carto.IMap=activeView.FocusMap

  ' Ensure the layer is available(layerIndex is 0-based)
  If layerIndex >= map.LayerCount Then
          Return
  End If

  ' Refresh and Zoom to the layer
  activeView.Extent=map.Layer(layerIndex).AreaOfInterest
  activeView.Refresh()

End Sub

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