Iterate through controls

I’m new to xojo and looking for how to iterate through the controls on a form in code. Does anyone have any examples?

Use the Window.ControlCount and Window.Control methods. Example:

Dim i, c As Integer Dim obj As Control c = Self.ControlCount - 1 For i = 0 To c Step 1 obj = Self.Control(i) Next

Note that you will have to coerce the type to do anything useful in most situations.

If obj IsA PushButton Then
Dim objButton As PushButton = PushButton(obj) PushButton stuff...
End If

Just what I needed. Thank you.

Would this work on web textfields on a WebContainerControl?
(I’m still using 2012 r2.1 enterprise edition)

It’s a little different for Web Apps. Look at the example on It’s using ControlCount and ControlAtIndex.

[quote=11794:@Brian O’Brien]Would this work on web textfields on a WebContainerControl?
(I’m still using 2012 r2.1 enterprise edition)[/quote]

Yes. But you use ControlAtIndex(i) instead of Control(i). And coerce to the web types such as WebButton instead of PushButton.

That is a confusing example. Why get the control by index, then search for it by name, when the name is the goal to begin with?

I think they’re doing this because the index based method returns a WebObject while the name based one returns an actual WebControl, thereby filtering some of the other stuff on the page. Which also explains the 2nd nil check. But that’s not explicit. It will break if the name based version ever changes the return type / behavior. Whoever wrote it also forgot to -1 the count as seen in the ControlCount example.

I know you know this Bob, but for others who might read this: explicitly test for the type(s) you want, using IsA, while iterating through controls. This covers Nil as well, i.e. you don’t need a separate Nil check.

To be honest, I didn’t look too closely at their example code. I just knew it had ControlCount and ControlAtIndex. Sorry for any confusion.

Why? You didn’t write it :wink:

Ha! Well, you’d be surprised what people say sometimes. You’d think I was employed, at times, by Xojo Inc.

No, what I meant was that I should have looked at the example and called it crap (my words not yours), like you did, and given a better example.

Indeed, that was a bad example. I’ve updated it with something simpler based on Daniel’s comment.

Talk about fast service!

Awesome, Paul. Thanks!


How do I read the value of the control when iterate through controls?
When I tried using this statement but I am getting an error.

Error: Type “WebControl” has no member named “Text”

Is there any workaround to retrieve the control value by iterate through controls?

Thank you.

it’s true: the webcontrol does not have any “text” property.
webtextarea has one, but not webcontrol.

so you should use :

if webcontrol(webobj) isa webtextarea then msgbox webtextarea(webobj).text end if

Hello Jean-Yves,

Thanks for your reply. I need to read the value of WebTextFields.

If webObj IsA WebTextField Then

Is there any workaround on that?

Hello Jean-Yves,

I think I have got the solution:-

If webObj IsA WebTextField Then

Thanks for your reply.

Alvin, when you post code, please select it and click the code icon on top of the editor: .

It makes it much more legible :

If webObj IsA WebTextField Then msgbox(WebTextField(WebObj).Text) End

Ok noted