# Iterate through Container control fields

**URL:** <https://forum.xojo.com/t/iterate-through-container-control-fields/30235>\
**Category:** Getting Started\
**Created:** [March 9, 2016, 6:36pm UTC](https://forum.xojo.com/t/iterate-through-container-control-fields/30235 "2016-03-09T18:36:35Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Stevenson](https://forum.xojo.com/letter_avatar_proxy/v4/letter/p/85f322/32.png) [@Paul\_Stevenson](https://forum.xojo.com/u/Paul_Stevenson)\
**Post date:** [March 9, 2016, 6:36pm UTC](https://forum.xojo.com/t/iterate-through-container-control-fields/30235/1 "2016-03-09T18:36:35Z")

</div>

Hi all,

I am loop through the “normal” fields on a window to get their index to use later (in the first case a listbox to add the values from an array) when referencing them but can’t seem to get indexes for the fields in a container control.

My code is here:

```auto
  Dim iMyListBox As Integer
  Dim iMyTextField As Integer
  For i As Integer = 0 to Self.ControlCount -1
    c = Window.Control(i)
    If c IsA Listbox Then
      If Self.Control(i).Name = pListBoxName Then
        iMyListBox = i
      End If
    End If
    If c Isa ContainerControl Then
      //Not sure what goes here
    End If
  Next
```

I have found some old examples which I can’t make work and cant find any example projects or details in the documentation.

If anyone can point to a good starting point I would be grateful 🙂

---

<div class="post-metadata">

**Author:** ![Jean-Yves\_Pochez](https://forum.xojo.com/user_avatar/forum.xojo.com/jean-yves_pochez/32/21769_2.png) [@Jean-Yves\_Pochez](https://forum.xojo.com/u/Jean-Yves_Pochez)\
**Post date:** [March 9, 2016, 7:53pm UTC](https://forum.xojo.com/t/iterate-through-container-control-fields/30235/2 "2016-03-09T19:53:29Z")

</div>

xojo-get the nth control or container control in a given window

```auto
Function ControlOrContainerControl(Extends win As Window, index As Integer) As Object
  If index > win.ControlCount - 1 Then
    Return Nil
  Else
    Dim c As Control = win.Control(index)
    If c IsA EmbeddedWindowControl Then
      Dim iterator As Runtime.ObjectIterator = Runtime.IterateObjects()
      Do Until Not iterator.MoveNext()
        If iterator.Current IsA ContainerControl Then
          If ContainerControl(iterator.Current).Graphics = EmbeddedWindowControl(c).Graphics Then
            Return ContainerControl(iterator.Current)
          End
        End
      Loop
    Else
      Return c
    End
  End
End Function
```

---

<div class="post-metadata">

**Author:** ![Jean-Yves\_Pochez](https://forum.xojo.com/user_avatar/forum.xojo.com/jean-yves_pochez/32/21769_2.png) [@Jean-Yves\_Pochez](https://forum.xojo.com/u/Jean-Yves_Pochez)\
**Post date:** [March 9, 2016, 7:57pm UTC](https://forum.xojo.com/t/iterate-through-container-control-fields/30235/3 "2016-03-09T19:57:20Z")

</div>

[code]Function ContainerControls(extends w as Window) As ContainerControl()  
'get a list of all the container controls in a window

dim theList() as ContainerControl  
dim o as Runtime.ObjectIterator = Runtime.IterateObjects

while o.MoveNext  
if o.Current isA ContainerControl and ContainerControl(o.Current).Window is w then  
theList.Append ContainerControl(o.Current)  
end if  
wend  
return theList

End Function  
[/code]

---

<div class="post-metadata">

**Author:** ![Paul\_Stevenson](https://forum.xojo.com/letter_avatar_proxy/v4/letter/p/85f322/32.png) [@Paul\_Stevenson](https://forum.xojo.com/u/Paul_Stevenson)\
**Post date:** [March 9, 2016, 9:33pm UTC](https://forum.xojo.com/t/iterate-through-container-control-fields/30235/4 "2016-03-09T21:33:04Z")

</div>

Hi Jean-Yves,

I see the methods give a list of all container controls but what I am looking for is a way to get the index of the controls in the container control so that I can reference them directly.

I know I can update the fields etc using their names but I am trying to loop through all controls on a window to perversely reduce the volume of code and enable me to re-use the code.

Cheers,

Paul

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [March 9, 2016, 10:05pm UTC](https://forum.xojo.com/t/iterate-through-container-control-fields/30235/5 "2016-03-09T22:05:57Z")

</div>

This gets the index of labels part of a Control Set placed on the window :

[code] dim result as string  
for i as integer = 0 to self.ControlCount-1  
if Self.Control(i) isa label then  
result = result + Self.Control(i).name + " "+str(Label(Self.Control(i)).Index ) + EndOfLine  
end if  
next

system.DebugLog result[/code]

The same code works within a ContainerControl.

---

<div class="post-metadata">

**Author:** ![Paul\_Stevenson](https://forum.xojo.com/letter_avatar_proxy/v4/letter/p/85f322/32.png) [@Paul\_Stevenson](https://forum.xojo.com/u/Paul_Stevenson)\
**Post date:** [March 9, 2016, 10:23pm UTC](https://forum.xojo.com/t/iterate-through-container-control-fields/30235/6 "2016-03-09T22:23:05Z")

</div>

HI Michel,

I have tested that on my window (multiple labels, text fields and a couple of container controls and the code just lists the labels on the window, not those “in” the container control.

I tired modifying it to :

```auto
  Dim Result As String
  for i as integer = 0 to self.ccExportDetailsMain.ControlCount-1
    if Self.ccExportDetailsMain.Control(i) isa label then
      result = result + Self.Control(i).name + " "+str(Label(Self.Control(i)).Index ) + EndOfLine
    end if
  next
  
  system.DebugLog result
```

Note - ccExportDetailsMain is one of the container controls with multiple labels and text fields in it.

But it still only listed the labels on the window then threw IllegalCastException error.

Cheers,

Paul

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [March 9, 2016, 10:35pm UTC](https://forum.xojo.com/t/iterate-through-container-control-fields/30235/7 "2016-03-09T22:35:47Z")

</div>

You don’t understand. A container control is in fact an embedded window.

To get the labels inside the container control, you must run the code in the container. Add a method to the container control that sets or returns the information you need, and call it when you encounter a ContainerControl

```auto
If self.control(i) isa EmbeddedWindowControl then
```

Note that you need to check for EmbeddedWindowControl, and not ContainerControl.

---

<div class="post-metadata">

**Author:** ![Paul\_Stevenson](https://forum.xojo.com/letter_avatar_proxy/v4/letter/p/85f322/32.png) [@Paul\_Stevenson](https://forum.xojo.com/u/Paul_Stevenson)\
**Post date:** [March 9, 2016, 10:52pm UTC](https://forum.xojo.com/t/iterate-through-container-control-fields/30235/8 "2016-03-09T22:52:38Z")

</div>

Michel,

You are spot on, I didn’t understand 🙂

It now makes sense and I have the code moving through the controls on both the window and container control. Brilliant!

Thanks to both of you for your patience.

Cheers,

Paul

---

<div class="post-metadata">

**Author:** ![Leigh\_Roberts](https://forum.xojo.com/user_avatar/forum.xojo.com/leigh_roberts/32/821_2.png) [@Leigh\_Roberts](https://forum.xojo.com/u/Leigh_Roberts)\
**Post date:** [July 3, 2019, 1:37am UTC](https://forum.xojo.com/t/iterate-through-container-control-fields/30235/9 "2019-07-03T01:37:14Z")

</div>

[quote=252252:@Jean-Yves Pochez][code]Function ContainerControls(extends w as Window) As ContainerControl()  
'get a list of all the container controls in a window

dim theList() as ContainerControl  
dim o as Runtime.ObjectIterator = Runtime.IterateObjects

while o.MoveNext  
if o.Current isA ContainerControl and ContainerControl(o.Current).Window is w then  
theList.Append ContainerControl(o.Current)  
end if  
wend  
return theList

End Function  
[/code][/quote]

Jean-Yves Pochez, this is 3 years old but just saw this and thought it interesting. Question: How can you (or even can you) do the same thing with a subclass of a ContainerControl? I was experimenting but couldn’t get it to work. The idea being I could then get to some of my added properties.

---

<div class="post-metadata">

**Author:** ![Jean-Yves\_Pochez](https://forum.xojo.com/user_avatar/forum.xojo.com/jean-yves_pochez/32/21769_2.png) [@Jean-Yves\_Pochez](https://forum.xojo.com/u/Jean-Yves_Pochez)\
**Post date:** [July 3, 2019, 6:08am UTC](https://forum.xojo.com/t/iterate-through-container-control-fields/30235/10 "2019-07-03T06:08:56Z")

</div>

replace `o.current isa containercontrol` with `o.current isa (mycontainercontrol)`

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 6:13pm UTC](https://forum.xojo.com/t/iterate-through-container-control-fields/30235/11 "2020-10-29T18:13:23Z")

</div>


