Iterate thru control set

How do I iterate thru a control set?

I have a control set named txtSearch that consists of 6 text input boxes. I want to do something like:

for i=0 to txtSearch.ubound
if txtSearch(i).text=“some value” then
do something…
end if
next i

When I code the above i get ‘This item does not exist’ error with ubound highlighted in yellow.

use “5” instead of txtSearch.ubound.

Control sets are not actually arrays, therefore they have no ubound. Keep track of how many exist separately, or iterate thru all of your controls using ‘isA’ to determine if a control belongs with your set.

Obviously using 5 would work, but I should not have to do that. I should be able to determine how many controls are part of the control set.

The control set has a ubound property, it appears to be unusable though.

Then file a feature request via Feedback. IMHO, you’d be wasting your time. Since Control sets are not arrays, I’m not sure Xojo, Inc can add a ubound property and, even if its possible its not practical as keeping track of the total is trivial.

I have a small project at:

which has a module called modEXTENSIONS which has a method MyUbound which extends the control class so you can use soemthing like…

for m = 0 to self.mycontrol(0).MyUbound
// In case this element does not exist…
if self.mycontrol(m) <> nil then
// …
//- do something with this self.mycontrol(m) control
// …
end if
next m

You have to use an existing element of the control array, rather than just the control array name,
as the .ubound host, but if that does not do it for you, you can just take apart the code in the module and adapt that.

Hope it helps

The indexes in a control set do not have to be consecutive, so the concept of Ubound does not apply in all contexts. It would be better to have a “for each” approach, or make a utility method that returns an array of the controls in the set.

There may well be a better approach, but that litle example does cope with the gaps in a sparse control array via the
if self.mycontrol(m) <> nil then… bit -

The .MyUbound simply returns the highest member index found.

But if you enter your control and then the dot . , Xojo offer the Ubound Method ???

a generic function to get all the existing indexes relative a one control

Function theIndexList(extends theRectControl as RectControl) As integer() dim vn() as integer if theRectControl.Index>-1 then Dim tn as String=Introspection.GetType(theRectControl).name dim w as Window=theRectControl.TrueWindow dim n as Integer=w.ControlCount-1 for i as integer=0 to n if Introspection.GetType(w.Control(i)).name=tn then vn.Append w.Control(i).Index end if next vn.Sort end if Return vn End Function

Since you can create and remove controls with code, index number could be not consecutive.
This function will return all the indexes or an empty array

It may let you enter that, but upon Run, it will tell you Ubound is an unknown item.

Try deleting one of the members in the middle of your set, and see why ubound would make no sense. Contrary to an array, you can very well have members 0,2, no 3, then 4 and 5. A control set is not an array, even if the syntax to call a control is similar. Roger Clary suggestion is the best alternative, which enables you to keep track of the members.

It figures out that the base type is something like “PushButton” (or whatever the control type is)
And it also realizes its a control set so internally it states the type as “Pushbutton()” - which you notice looks like an array and the mistake follows from that.
But that can be fixed :slight_smile: