I've to regenerate a single layout with data from each point feature. So, I wrote a VB.NET routine to clean the layout, searching for text element, selecting it and deleting it. Well, the routine works just for some element and not for others.... Why?

Private Sub CleanLayout()

    Dim graphC As IGraphicsContainer = mxDocument.PageLayout
    Dim graphCSel As IGraphicsContainerSelect = graphC
    Dim elem As IElement = graphC.Next
    While elem IsNot Nothing
        If TypeOf elem Is ESRI.ArcGIS.Carto.ITextElement Then
            Dim elemProps As IElementProperties = DirectCast(elem, IElementProperties)
            If String.Compare(elemProps.Name, "ELEM_VAR", True) = 0 Then
                graphCSel = graphC
            End If
        End If
        elem = graphC.Next
    End While

    graphC = Nothing
    graphCSel = Nothing

End Sub

The result is that there is still some text element named "ELEM_VAR" on the layout....

Thanks in advance for any help. Software: ArcGIS 10 SP1 + VS2010

1 Answer 1


That's probably because you delete elements while you are iterating through them at the same time.

Consider iterating through the elements and add the ones you want to delete to a list first. You can use esriSystem.Array for that, for example. Then you can loop through that list and delete elements safely.

Also, there is no need to select an element before you delete it (i.e. you can lose the graphCSel.SelectElement(elem) statement).

  • Thanks Petr. I've already solved adding graphC.Reset() after graphC.DeleteElement(elem).
    – albus2011
    Mar 9, 2012 at 10:55
  • That will work, but can be very inefficient for a larger number of elements, since you will end up iterating multiple times over most of the elements.
    – Petr Krebs
    Mar 9, 2012 at 13:30

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.