I need to create custom tool for ArcMap, which would let the user delete polygons by mouse clicks. I am pretty new in this thing, so how can I retrieve a selected polygon so I could delete it? Plus do I need to load some sort of function to renew the view?

  • There is a related post here which may be of some use. – Joseph Oct 29 '14 at 12:36
  • thanks, but that code is missing some parts which I don't know where to get, because as I try to use tool, I get object is null reference error – Vilius Oct 29 '14 at 14:16
  • Ahh, then hopefully someone more qualified than I can advise. – Joseph Oct 29 '14 at 14:17
  • I'm not sure how you want your tool to work. Will you just click on one polygon to make it go away, or will you first select some polygons and then use your tool? Will you use it in an edit session or will it start its own edit session and close it with save edits? My answer below only answers your specific questions. – Stefan Oct 29 '14 at 15:43
  • First option, after tool activation any clicked polygon should be removed – Vilius Oct 29 '14 at 16:38

Edit: completely rewrote answer since i cut and pasted together c# and VB.NET code by mistake.

This is how a onCreate typically looks like:

 public override void OnCreate(object hook)
      if (hook == null)

      m_application = hook as IApplication;
      if (m_application == null)

      UID uID = new UID();
      uID.Value = "esriEditor.Editor";
      m_editor = m_application.FindExtensionByCLSID(uID) as IEditor;

      if (m_editor == null)

      m_editSketch = m_editor as IEditSketch;

From the member variable m_editor you can access the selection using IEditor3 interface. From member variable m_application you can access ActiveView to do the refresh.

Another solution would be to call the ControlsEditingCutCommand command from your tool. Yes, a tool can call a command. The cut command will as a side effect put the polygon on the clipboard. It just cuts the current selection, so what your tool needs to do is to create a selection when clicked and then invoke the cut command. This will save you all the hassle with acutally deleting the feature from the featureclass.

Your tool would then in onMouseUp select in the map what you are clicking using IMap.SelectByShape(). You have to create the input to SelectByShape by creating a shape form the X and Y arguments. X and Y are in screen coordinates, so you have to transform them to map coordinates. When you have a selection, you just call the cut tool.

I've stripped down a similar tool to do what you are looking for. Sorry it's in VB, but my original code was written that way. It is however pretty straight forward to translate. You must be in an edit session for it to work and you might want to add some code to make it only delete from a special layer.

Public Overrides Sub OnMouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)

            Dim r As ESRI.ArcGIS.esriSystem.tagRECT
            Dim pixelTol As Integer = 5
            'Construct a small rectangle out of the x,y coordinates' pixel tolerance.
            r.left = X - pixelTol 'Upper left x, top left is 0,0.
            r.top = Y - pixelTol 'Upper left y, top left is 0,0.
            r.right = X + pixelTol 'Lower right x, top left is 0,0.
            r.bottom = Y + pixelTol 'Lower right y, top left is 0,0.

            'Transform the device rectangle into a geographic rectangle via the display transformation.
            Dim pEnvelope As IEnvelope = New EnvelopeClass()
            Dim pActiveView As IActiveView = m_application.ActiveView
            Dim pDisplayTrans As IDisplayTransformation = pActiveView.ScreenDisplay.DisplayTransformation
            pDisplayTrans.TransformRect(pEnvelope, r, 5) '5 = esriTransformPosition + esriTransformToMap.
            Dim map As IMap = m_application.FocusMap
            map.SelectByShape(pEnvelope, Nothing, True)

            Dim deleteCommand As ICommand = New ControlsEditingCutCommand()

        End Sub
  • Could you give me link to example event if it is visual basic, because I can't initialize few of the components right – Vilius Oct 29 '14 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.