Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

WPF remove PointAnnotations from graph

Solved!
Go to solution

Hi

I want to remove all my PointAnnotations in a graph. I have tried the code below but it doesn't work. Is there som other way?

Thanks

/Axel

       

For i = 0 To sg.Children.Count - 1

If sg.Children(i).GetType = GetType(PointAnnotation) Then

                sg.Children.Remove(i)

EndIf

Next

0 Kudos
Message 1 of 3
(5,504 Views)
Solution
Accepted by topic author AxelHaraldsson

There are two issues with the code as written:


  • First, it is using the Remove method on ObjectCollection instead of the RemoveAt method (i.e. it is trying to remove the object i, not the element at index i).
  • Second, when you remove an element at index i, all the later elements in the collection effectively shift down one, but the For loop still increments i before checking the next element, skipping adjacent values.

When removing elements, it is best to iterate backwards over the collection to avoid index mis-alignment:


    For i = sg.Children.Count - 1 To 0 Step -1
        If sg.Children(i).GetType = GetType(PointAnnotation) Then
            sg.Children.RemoveAt(i)
        End If
    Next

~ Paul H
0 Kudos
Message 2 of 3
(5,498 Views)

Thanks Smiley Happy

/Axel

 

0 Kudos
Message 3 of 3
(5,492 Views)