ArcObjects Library Reference

Get Selected Feature Layer in Contents View Snippet

Returns a reference to the currently selected featurelayer in the given contents view.

[C#]

///<summary>Returns a reference to the currently selected featurelayer in the given contents view.</summary>
///  
///<param name="currentContentsView">An IContentsView interface.</param>
///  
///<returns>An IFeatureLayer interface or Nothing if not found.</returns>
///   
///<remarks></remarks>
public ESRI.ArcGIS.Carto.IFeatureLayer GetSelectedFeatureLayerInContentsView(ESRI.ArcGIS.ArcMapUI.IContentsView currentContentsView)
{
    
  if (currentContentsView != null && currentContentsView.SelectedItem is ESRI.ArcGIS.Carto.IFeatureLayer)
  {
    ESRI.ArcGIS.Carto.IFeatureLayer featureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)currentContentsView.SelectedItem; // Explicit Cast

    return featureLayer;
  }
  return null;
}
[Visual Basic .NET]

'''<summary>Returns a reference to the currently selected featurelayer in the given contents view.</summary>
'''  
'''<param name="currentContentsView">An IContentsView interface.</param>
'''  
'''<returns>An IFeatureLayer interface or Nothing if not found.</returns>
'''   
'''<remarks></remarks>
Public Function GetSelectedFeatureLayerInContentsView(ByVal currentContentsView As ESRI.ArcGIS.ArcMapUI.IContentsView) As ESRI.ArcGIS.Carto.IFeatureLayer

  If Not currentContentsView Is Nothing AndAlso TypeOf currentContentsView.SelectedItem Is ESRI.ArcGIS.Carto.IFeatureLayer Then
	  Dim featureLayer As ESRI.ArcGIS.Carto.IFeatureLayer = CType(currentContentsView.SelectedItem, ESRI.ArcGIS.Carto.IFeatureLayer) ' Explicit Cast

    Return featureLayer

  End If

  Return Nothing

End Function


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