Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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
    graphCSel.UnselectAllElements()
    graphC.Reset()
    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
                graphCSel.SelectElement(elem)
                graphC.DeleteElement(elem)
            End If
        End If
        elem = graphC.Next
    End While

    mxDocument.ActiveView.Refresh()
    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

share|improve this question
add comment

1 Answer

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).

share|improve this answer
    
Thanks Petr. I've already solved adding graphC.Reset() after graphC.DeleteElement(elem). –  albus2011 Mar 9 '12 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 '12 at 13:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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