locked
For Each .... RRS feed

  • Question

  • Hi, 

    I'm currently using the following for loop to go tru each button

    For Each c As Button In g1.Children.OfType(Of Button)()
    next

    It works fine, but I was wondering if there is a way to change the order it goes thru the button? Like if currently it starts at button x and ends at button y, how can i make it start at button y ?

    Thank You


    • Edited by Me Man Saturday, December 28, 2013 12:46 AM
    Saturday, December 28, 2013 12:45 AM

Answers

  • No, the order isn't customizable with for each. You can iterate through the collection by index however you'd like. For example, you can use a For loop:

            Dim buttons = g1.Children.OfType(Of Button)()
            Dim startAt As Integer = 5
            Dim numButtons = buttons.Count
            For i As Integer = 0 To numButtons - 1
                Debug.WriteLine(buttons((startAt + i) Mod numButtons).Content.ToString())
            Next

    --Rob

    Saturday, December 28, 2013 12:59 AM
    Moderator
  • Hi, 

    Here is a solution i found 

    For Each c As Button In g1.Children.OfType(Of Button).reverse()
    next

    the Reverse keyword did the trick. 

    Thanks

    • Marked as answer by Me Man Saturday, December 28, 2013 1:15 AM
    Saturday, December 28, 2013 1:15 AM

All replies

  • No, the order isn't customizable with for each. You can iterate through the collection by index however you'd like. For example, you can use a For loop:

            Dim buttons = g1.Children.OfType(Of Button)()
            Dim startAt As Integer = 5
            Dim numButtons = buttons.Count
            For i As Integer = 0 To numButtons - 1
                Debug.WriteLine(buttons((startAt + i) Mod numButtons).Content.ToString())
            Next

    --Rob

    Saturday, December 28, 2013 12:59 AM
    Moderator
  • Hi, 

    Here is a solution i found 

    For Each c As Button In g1.Children.OfType(Of Button).reverse()
    next

    the Reverse keyword did the trick. 

    Thanks

    • Marked as answer by Me Man Saturday, December 28, 2013 1:15 AM
    Saturday, December 28, 2013 1:15 AM