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:
  1. Add a new UIToolControl to any toolbar.
  2. Paste the code into VBA.
  3. Make sure the names of the controls match, the sample assumes UIToolControl1.
  4. Completely close VBA so mouse events fire.
  5. Select the tool and then click on a feature in the focus map.
Private Sub UIToolControl1_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long)
  Dim pMxApp As IMxApplication
  Dim pMxDoc As IMxDocument
  Dim pMap As IMap
  Dim pActiveView As IActiveView
  Dim pEnvelope As IEnvelope
  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, False
  'Refresh the old selection to erase it
  pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing
  'Perform the selection using a point created on mouse down
  pMap.SelectByShape pEnvelope, pMxApp.SelectionEnvironment, True
  'Refresh again to draw the new selection
  pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing
End Sub