Loop through controls

Need to loop through controls in self created container… So I was trying something like:

For Each control in MyToolbar (this is my container)


Next
But it seems like that is not it.

I am new to Xojo…

Welcome to Xojo… :wink:

Something like this might do the trick…

  Dim i As Integer
  
  i = 0
  while i < MyToolbar.ControlCount
    MsgBox MyToolbar.Control(i).Name
    i = i + 1
  wend 

That’s how we do it (and I believe is also the most recommended way!) You can also use the ‘isa’ operator - something like

If MyToolbar.Control(i) isa TextArea

to test the types of control, so you can then do this:

TextArea(MyToolbar.Control(i).Text) = "Fish"

and so on.

You can use Isa with select case like this:

dim i as uint32

  while i < MyToolbar.ControlCount
select case myToolbar.Control(i)
case isa TextArea
TextArea(MyToolbar.Control(i)).Text = "Fish"
case isa Rectangle, Circle
Rectangle(myToolbar.Control(i)).Width = 12
case else
//not a textarea, circle or rectangle
end select
i = i + 1

Not tested the code above and I believe Hamish made an error. I you use to make this mistake when I started out with casting. You would write TextArea(MyToolbar.Control(i)).Text = “Fish” instead of TextArea(MyToolbar.Control(i).Text) = “Fish”

This is because we are casting the control and not the text.

Both Alwyn and Hamish have made good contributions. I am just adding to what they said and putting it together for you.

Hope this helps

Ooops…forgot end while sorry. You might need to test this code first as I have not tested it. Good luck.

dim i as uint32

  while i < MyToolbar.ControlCount
select case myToolbar.Control(i)
case isa TextArea
TextArea(MyToolbar.Control(i)).Text = "Fish"
case isa Rectangle, Circle
Rectangle(myToolbar.Control(i)).Width = 12
case else
//not a textarea, circle or rectangle
end select
i = i + 1
end while

Not tested the code above and I believe Hamish made an error. I you use to make this mistake when I started out with casting. You would write TextArea(MyToolbar.Control(i)).Text = “Fish” instead of TextArea(MyToolbar.Control(i).Text) = “Fish”

This is because we are casting the control and not the text.

Both Alwyn and Hamish have made good contributions. I am just adding to what they said and putting it together for you.

Hope this helps

Yep. Pesky bracket.

Thanks everybody - I used this:

Dim i as Integer
Dim X as WebObject
For i=0 to Me.ControlCount - 1
if Me.ControlAtIndex(i) isa weblabel Then
WebLabel(Me.ControlAtIndex(i)).Style = styToolBarItem
end if
Next

and it did set the style for all WebLabels

I have several ContainerControls added programmatically and every container has a PopUpMenu. I want to read the text in every PopUpMenu. My CCList contains every added ContainerControl. I came this far:

for i as Integer = 0 to CCList.Ubound for j as Integer = 0 to CCList(i).ControlCount select case CCList(i).Control(j) case isa PopupMenu ---> here I want to retrieve the value of the PopUpMenu end select next j next i

I can’t access the Text property…??

Have you tried

myString = PopupMenu(CCList(i).Control(j)).Text

(declaring myString as string elsewhere)
?

[quote=98795:@Hamish Symington]Have you tried

myString = PopupMenu(CCList(i).Control(j)).Text

(declaring myString as string elsewhere)
?[/quote]

Yes! Although I find the “myString = PopupMenu(CCList(i).Control(j)).Text” somewhat illogical. Thanks.

Alexander, why do u think it is illogical??

i usually use for-next to loop through the controls. is there any reason why you use the while-wend loop, Hamish??

Well… illogical… I didn’t know that Controls could be enumerated like that…

Quicker than writing a for loop? Depends how you prefer to write a for loop though. Wether it is like this:

dim i as integer
for i = 0 to 12
next

instead of this:
for i as integer = 0 to 12
next

Hmm…maybe not quicker actually but it’s most likely just personal preference.