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


Zoom to Active Layer in TOC Snippet (ArcObjects .NET 10.5 SDK)
ArcObjects Library Reference

Zoom to Active Layer in TOC Snippet

Zooms to the selected layer in the Table of Contents (TOC) associated with the active view.

[C#]
///<summary>Zooms to the selected layer in the TOC associated with the active view.</summary>
/// 
///<param name="mxDocument">An IMxDocument interface</param>
///  
///<remarks></remarks>
public void ZoomToActiveLayerInTOC(ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument)
{
  if(mxDocument == null)
  {
    return;
  }
  ESRI.ArcGIS.Carto.IActiveView activeView=mxDocument.ActiveView; 

  // Get the TOC
  ESRI.ArcGIS.ArcMapUI.IContentsView IContentsView=mxDocument.CurrentContentsView;

  // Get the selected layer
  System.Object selectedItem=IContentsView.SelectedItem;
  if (!(selectedItem is ESRI.ArcGIS.Carto.ILayer))
  {
    return;
  }
  ESRI.ArcGIS.Carto.ILayer layer=selectedItem as ESRI.ArcGIS.Carto.ILayer; 


  // Zoom to the extent of the layer and refresh the map
  activeView.Extent=layer.AreaOfInterest;
  activeView.Refresh();
}
[Visual Basic .NET]
'''<summary>Zooms to the selected layer in the TOC associated with the active view.</summary>
''' 
'''<param name="mxDocument">An IMxDocument interface</param>
'''  
'''<remarks></remarks>
Public Sub ZoomToActiveLayerInTOC(ByVal mxDocument As ESRI.ArcGIS.ArcMapUI.IMxDocument)

  If mxDocument Is Nothing Then
    Return
  End If

  ' Get the map
  Dim activeView As ESRI.ArcGIS.Carto.IActiveView=mxDocument.ActiveView

  ' Get the TOC
  Dim contentsView As ESRI.ArcGIS.ArcMapUI.IContentsView=mxDocument.CurrentContentsView

  ' Get the selected layer
  Dim selectedItem As System.Object=contentsView.SelectedItem
  If Not (TypeOf selectedItem Is ESRI.ArcGIS.Carto.ILayer) Then
    Return
  End If

  Dim layer As ESRI.ArcGIS.Carto.ILayer=TryCast(selectedItem, ESRI.ArcGIS.Carto.ILayer) ' Dynamic Cast

  ' Zoom to the extent of the layer and refresh the map
  activeView.Extent=layer.AreaOfInterest
  activeView.Refresh()

End Sub

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