Iterate through Container control fields

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:

  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 :slight_smile:

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

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

[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]

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

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.

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 :

  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

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

If self.control(i) isa EmbeddedWindowControl then

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

Michel,

You are spot on, I didn’t understand :slight_smile:

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

[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.

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