[GIS] Implementing Custom Selection Tool (Adding to current selection) with ArcObjects

arcgis-9.3arcmaparcobjectsc

I am trying to implement a custom selection tool in ArcMap 9.3.1 that is functionally similar to the default ArcMap selection tool with "Adding to current selection" enabled. ie. the user can click to incrementally select individual features one by one, while adding on to previously selected features. When the user clicks on an already selected feature, that feature should be unselected.

How should I go about replicating and implementing this behavior for my custom tool? There are some selection-related code snippets lying around, but nothing really comes close. I was able to perform single selections by calling SelectByShape(geom, null, true), where geom = a small area around the clicked point. To support multiple selections at various locations, how then do I keep the previously selected features still selected as I select a new one, since SelectByShape(geom, null, true) only selects a single feature each time it's called.

Clearly, I will need another approach, which probably involves me keeping a growing list of currently selected features, but I am not sure if there is a more straightforward solution. Has anyone attempted this before?

Best Answer

I believe in your scenario, you can keep on using SelectByShape method within the following sequence:

  1. Access IMxApplication.SelectionEnvironment, save the value of ISelectionEnvironment.CombinationMethod to a temporary variable.

  2. Set ISelectionEnvironment.CombinationMethod to esriSelectionResultEnum.esriSelectionResultXOR. This will ensure proper handling of already selected features, which will be unselected when part of the new selection.

  3. Pass the application's selection environment to SelectByShape, calling the method as you already do. SelectByShape will use the selection environment selection method you set previously. Consider using justOne=false to support adding/removing multiple features at a time.

  4. Restore the selection environment's combination method to that saved aside in the first step.