Loop through WebControls by TabIndex

Hi,

I understand For Each ctl As WebUIControl In Controls iterates through all controls in a container, but I want to process them in a specific order,

I’ve managed to build an array of webcontrol.controlIDs sorted by tab order…

var CtlID(999) as String
var i as Integer = 0
For Each ctl As WebUIControl In Controls
  CtlID(ctl.TabIndex) = ctl.ControlID
  i = max(i,ctl.TabIndex)
Next

How can I loop through the array processing each control in the taborder?

You should build and retain your own array of references. The order will be as you set it.

Rule of thumb is any time you are iterating the Controls array, you are doing something somewhere wrong.

Tim,

I’ve built the array, but I can’t work out how to interate through it. All the examples I can find use:

For Each ctl As WebUIControl In Controls

I want/need

var CtlID(999) as String
var i as Integer = 0
For Each ctl As WebUIControl In Controls
  CtlID(ctl.TabIndex) = ctl.ControlID
  i = max(i,ctl.TabIndex)
Next

For j = 0 to i      'i = highest tabindex
    reference the control by CtlID(j)
next

Looking to use this with reference to Ricardo’s Vertically Responsive Text Controls example

you could collect the objects and then sort the array by TabIndex
see .Sort(AddressOf methodName)
a simple compare method.

I’ve done that - now want to loop thru the array addressing each control…

Public Function ControlTabIndexOrder(c1 As WebUIControl, c2 As WebUIControl) As Integer
  If c1.TabIndex > c2.TabIndex Then Return 1
  If c1.TabIndex < c2.TabIndex Then Return -1
  return 0
End Function

Sub Pressed() Handles Pressed
  Var list() As WebUIControl
  list.RemoveAll
  
  For Each ctl As WebUIControl In Controls
    list.Add(ctl)
    System.DebugLog ctl.TabIndex.ToString
  Next
  
  System.DebugLog "Sort"
  
  list.Sort(AddressOf ControlTabIndexOrder)
  
  For Each ctl As WebUIControl In list
    System.DebugLog ctl.TabIndex.ToString
    If ctl IsA WebCheckbox Then
      Var check As WebCheckbox = WebCheckbox(ctl)
      System.DebugLog check.Value.ToString
    End If
  Next
  
End Sub

I’ll give this a go - thanks.