Display only selected features in ArcMap using ArcObjects

Last Published: April 25, 2020


Instructions provide sample code which will display only a selected set of features from a given feature class.


  1. Start ArcMap.
  2. Open the Visual Basic Editor.

    In ArcMap, select Tools > Macros > Visual Basic Editor.

  3. In the Project Explorer window, expand the Project folder, select ThisDocument, and right-click to select View Code.
    [O-Image] Visual Basic Project  Explorer
    Code in the Project's ThisDocument code module will only run in the current map document. If you want to store the code in all your map documents, open the Normal.mxt ThisDocument code module instead.

  4. Paste the following code into the code module

    Sub Main()

    Dim pDoc As IMxDocument
    Dim pLayer As IGeoFeatureLayer
    Dim pFS As IFeatureSelection
    Dim pSS As ISelectionSet

    Set pDoc = Application.Document

    'Make certain that the layer is selected in the TOC
    Set pLayer = pDoc.SelectedLayer
    Set pFS = pLayer 'QI
    Set pSS = pFS.SelectionSet

    DisplayOnlySelectedFeatures pLayer, pSS

    'Refresh Data Frame

    End Sub

    Public Sub DisplayOnlySelectedFeatures(pGlayer As IGeoFeatureLayer _
    , pSelectionset As ISelectionSet)

    'Declare variables
    Dim pFCEntire As IFeatureCursor
    Dim pSelFC As IFeatureCursor
    Dim lEntireOID As Long
    Dim lSelOID As Long
    Dim pFeatureIDSet As IFeatureIDSet
    Dim pEntireEnumId As IEnumIDs
    Dim pSelEnumIds As IEnumIDs
    Dim pEntireSelSet As ISelectionSet

    'set feature cursors for entire feature class and the selectionset
    Set pFCEntire = pGlayer.FeatureClass.Search(Nothing, False)
    pSelectionset.Search Nothing, False, pSelFC

    'Enumerate through the selectionsets to get the ids
    Set pSelEnumIds = pSelectionset.IDs
    lSelOID = pSelEnumIds.Next
    Set pEntireSelSet = pGlayer.FeatureClass.Select(Nothing, _
    esriSelectionTypeHybrid, esriSelectionOptionNormal, Nothing)
    Set pEntireEnumId = pEntireSelSet.IDs
    lEntireOID = pEntireEnumId.Next
    Set pFeatureIDSet = New FeatureIDSet

    'Loop through Entire feature layer's oids and
    'write them to the featureidset
    Do Until lEntireOID = -1
    pFeatureIDSet.Add lEntireOID
    lEntireOID = pEntireEnumId.Next

    'Loop through the selected feature's oids and check to see
    'if the featureidset contains any of them.
    'If so, remove them.
    Do Until lSelOID = -1
    If pFeatureIDSet.Contains(lSelOID) Then
    pFeatureIDSet.Remove (lSelOID)
    End If
    lSelOID = pSelEnumIds.Next

    'Set the exclusion featureidset to that of the geofeaturelayer
    Set pGlayer.ExclusionSet = pFeatureIDSet

    End Sub

  5. Close the Visual Basic Editor.
  6. Add a feature class to ArcMap and select some features.
  7. Select the layer in the table of contents.
  8. Run the code.

    A. Click Tools > Macros > Macros to display the Macros dialog box.
    B. Select a macro and click Run.

    These features can be restored by right-clicking on the layer in the Table of Contents and selecting Properties. On the Display tab, select Feature Exclusion > Restore All.

Article ID:000006282

  • ArcMap 8 x
  • ArcMap 9 x

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options