English

How To: Update Attributes window when selecting features programmatically

Summary

The Attributes Inspector window updates automatically when a user selects a feature with a tool on the map. However, this may not be true when a feature is selected programmatically.

Procedure

The following sample illustrates how to select a feature programmatically and to update the Attributes Inspector. The key is to use ISelectionEvents to notify the Attributes Inspector.
 

  1. Start ArcMap.
  2. Create a new UIButtonControl: How To: Create a new UIButtonControl
    Note:
     For more information on creating a UIControl, see the ArcGIS Desktop Help topic 'Creating custom commands with VBA and UI Controls'
  3. Right-click the UIButtonControl and select View Source.
  4. Copy this code into the UIButtonControl's click event.
     
    Code:
    Dim pMxDoc As IMxDocument
    Dim pMap As IMap
    Dim pFSel As IFeatureSelection
        
    ' Build a query to select the first feature
    Dim pQF As IQueryFilter
    Set pQF = New QueryFilter
    pQF.WhereClause = "Fid = 1"
        
    
    Dim pFeatCur As IFeatureCursor
        
    ' Return a reference to the map
    Set pMxDoc = ThisDocument
    Set pMap = pMxDoc.FocusMap
    
    ' Clear the prior selection
    pMap.ClearSelection
        
    ' Select a feature
    Set pFSel = pMap.Layer(0)
    pFSel.SelectFeatures pQF, esriSelectionResultNew, False
        
    'Refresh the Active View
    Dim pAV As IActiveView
    Set pAV = pMap
    pAV.PartialRefresh 2, pFSel, Nothing
    ' **************************************
    

     
  5. Click AddData to load one editable layer.
  6. From the Tools Menu, click on the Editor toolbar.
  7. Click Editor > Start Editing.
  8. On the Editor toolbar, click the Attributes button to open the Attributes Inspector.
  9. Click on the new button to run the code.
  10. Notice the Attributes Inspector is empty; this occurs even though a feature has been selected because ISelectionEvents::SelectionChanged was not called.
  11. Copy this code into the UIButtonControl's click event, just before End Sub.
     
    Code:
    Dim pSelEvents As ISelectionEvents
    Set pSelEvents = pMap
    pSelEvents.SelectionChanged
    
  12. Click on the button to run the code. The code has programmatically selected a feature and updated the Attributes Inspector.