Iterating through WebContainer controls

The documentation doesn’t give any examples for iterating WebControl controls (WebTextField, WebRadioGroup etc) in a WebContainer.

WebContainer — Xojo documentation

I attempted the following in a method of the WebContainer, but it’s only iterating through WebPagePanel, WebButton and WebSegmentedButton. Why is it not picking up other type of controls stated above?

For Each control As WebControl In Self.Controls
  If control IsA WebTextField Then
    WebTextField(control).MyMethod
  ElseIf control IsA WebPopupMenu Then
    WebPopupMenu(control).MyMethod
  ElseIf control IsA WebRadioGroup Then
    WebRadioGroup(control).MyMethod
  ElseIf control IsA WebDatePicker Then
    WebDatePicker(control).MyMethod
  End If
Next

are the controls on a other level?
set a break point and look at the debug window.

I have a WebPagePanel insider the WebContainer. All other controls are placed in the WebPagePanel. So that could be why? But I can’t find a Controls property of WebPagePanel.

Thanks - Issue now sorted. It looks like I need a for loop using ControlAt and LastControlIndex as not all objects have a Control property.

Care to share the updated code? Please and thanks! :smiley:

Sure, no problem. Assuming I run the code from a control event:

Var control As WebControl

For ControlIndex As Integer = 0 To Self.LastControlIndex
  control = Self.ControlAt(ControlIndex)
  If control IsA WebTextField Then
    WebTextField(control).CallSomeMethod
  ElseIf control IsA WebPopupMenu Then
    WebPopupMenu(control).CallSomeMethod
  ElseIf control IsA WebRadioGroup Then
    WebRadioGroup(control).CallSomeMethod
  ElseIf control IsA WebDatePicker Then
    WebDatePicker(control).CallSomeMethod
  End If
Next

Just need to bear in mind, if a control has child controls, within which are more controls you want to access, you have to iterate through each parent. e.g. if WebPagePanel contained a WebTextField and a WebRectangle, and inside that WebRectangle there is another WebTextField, I believe you have to iterate through both the WebPagePanel and the WebRectangle to get to both WebTextField. The way I do this is to store parent controls in an Array. Then I loop through the array and run the above code in an inner loop.

2 Likes

Perhaps you can mark it as the solution for others that might search for this in the future.

Thank you!

if it is in last xojo version it is worth a feature request to have .controls there too to be consistent.