How To: Update Attributes window when selecting features programmatically
Note: Support for Visual Basic for Applications (VBA) for ArcMap and ArcCatalog ended with the ArcGIS 10.2.2 release, and Esri has not included VBA compatibility setups since version 10.5. See: ArcGIS Desktop and VBA Moving Forward
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.
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.
- Start ArcMap.
- 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'
- Right-click the UIButtonControl and select View Source.
- Copy this code into the UIButtonControl's click event.
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 ' **************************************
- Click AddData to load one editable layer.
- From the Tools Menu, click on the Editor toolbar.
- Click Editor > Start Editing.
- On the Editor toolbar, click the Attributes button to open the Attributes Inspector.
- Click on the new button to run the code.
- Notice the Attributes Inspector is empty; this occurs even though a feature has been selected because ISelectionEvents::SelectionChanged was not called.
- Copy this code into the UIButtonControl's click event, just before End Sub.
Dim pSelEvents As ISelectionEvents Set pSelEvents = pMap pSelEvents.SelectionChanged
- Click the button to run the code. The code has programmatically selected a feature and updated the Attributes Inspector.