I am writing a small application that allows the user to click on the map, and it will create 3 buffers (these are graphics using the IElement interface) at specified distances. What I want to do is if there are buffers on the map from the previous click, and the user clicks on the map to create another set of buffers, delete the oldest set of buffers (which are graphics) So in essence at anyone time there should only be 3 buffers on the map.

Has anyone got any ideas on the best way to do this?



You can delete graphics through the IGraphicsContainer interface, either DeleteElements or DeleteAllElements method. See sample code below.

Delete Graphics Refresh Active Snippet

Here is a Delete Graphic by Name VBA sample.

Public Sub DeleteGraphicsByName(ByVal pMxDoc As IMxDocument, ByVal sElementName As String, Optional ByVal bRefresh As Boolean = False)

    'Delete the named graphics from the gra container
    Dim pGraCon As IGraphicsContainer
    Dim pElementProp As IElementProperties
    Dim pElement As IElement

    ' Get the graphics container
    pGraCon = pMxDoc.FocusMap

    'Search for and delete the previous graphic by name
    pElementProp = pGraCon.Next
    Do Until pElementProp Is Nothing
      If pElementProp.Name = sElementName Then
        pElement = pElementProp
      End If
      pElementProp = pGraCon.Next

    If bRefresh Then
      pMxDoc.ActiveView.PartialRefresh(esriViewGraphics, Nothing, Nothing)
    End If

  End Sub

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.