|Select By Location
This sample uses a UIToolControl to select one feature in the focus map. The code uses
IMap::SelectByShape to perform the selection which requires a geometry to find
features in the map. The geometry the sample uses is a new point created using the coordinates
passed into the mouse down event. Any geometry will do however, see the 'Zoom Focus Map'
tip for an example that uses a RubberEnvelope object to track a new envelope.
SelectByShape also requires a selection environment. The sample uses the selection
environment currently associated with the application. For this reason the code may not
actually select features depending on the settings in the selection environment. For
example, a feature layer may not be selectable, the selection environment may bet set
to remove features from the selection rather than create a new selection, etc. In the
ArcMap application, access to the selection environment is under the Selection menu.
How to use:
- Add a new UIToolControl to any toolbar.
- Paste the code into VBA.
- Make sure the names of the controls match, the sample assumes UIToolControl1.
- Completely close VBA so mouse events fire.
- Select the tool and then click on a feature in the focus map.
Private Sub UIToolControl1_MouseDown(
Set pMxApp = Application
Set pMxDoc = Application.Document
Set pMap = pMxDoc.FocusMap
Set pActiveView = pMap
Set pEnvelope = pMxDoc.CurrentLocation.Envelope
pEnvelope.Expand pMxDoc.SearchTolerance, pMxDoc.SearchTolerance,
'Refresh the old selection to erase it
'Perform the selection using a point created on mouse down
pMap.SelectByShape pEnvelope, pMxApp.SelectionEnvironment,
'Refresh again to draw the new selection