Referring to Controls in Code

I am trying to populate multiple textboxes using code.

I have 10 textboxes, named txtData1, txtData2, txtData3,…txtData10. I am try to set up a for loop that refers to the textboxes from 1 to 10.

I know that in VB6 it could be done like txtData(n)= data

How can I do this in Xojo.

Check out control sets

Yes, check out that link Norman posted above.

One thing I will point out is to name the first control to the name you want to refer to, then assign all other new members to that set. Otherwise, I couldn’t see any way of renaming the set later.

Thanks for your answers. I think I might need to get a little more specific.

I have created created a method that uses the same code for different departs. Let’s say Steel and Welding. Each dept. has 3 labels and 3 textboxes. They are named lblSteel1,…lblSteel3 and txtSteel1,…txtSteel3. So replacing the ‘Steel’ with ‘Weld’ would give the name of the Welding dept. controls.

So using code can I create a variable, that will allow me to refer to a specific control?

I suspect what you’re trying to do is something like

dim ctrl as Control = "lbl" + "steel" + "1"

programmatically to dynamically compute the name of a control and then have a reference to it

And no you really cant do that

BUT what you can do is compute the name then go through the controls on a layout & find that one with that name

    dim ctrlName as string = "lbl" + "steel" + "1"
    dim found as boolean
    for i as integer = 0 to ControlCount() -1
            if controls(i).Name = ctrlName then
                   // FOUND the one we wanted !!!!!!
                  found = true
            end if
    next
    
   if not found then
      // went through all the controls on the layout & did not find the one of interest
   else
      // we did find the one with that name
   end if

see http://documentation.xojo.com/index.php/Window.ControlCount

Thanks Norman.

That worked ok. So now that I have the control number, can I then use that number to assign text to the label?

I have tried, but there is no option to use control(3).text = “data”

use this function to get any control by it’s name. then you can set anything in the control by code

[code]Public Function GetControlByName(aWindow as window, aName as String) as RectControl
dim ctrl as RectControl

if aWindow<>nil then
For i as Integer=0 To aWindow.ControlCount-1
if aWindow.control(i) isa rectcontrol then
ctrl=RectControl( aWindow.control(i))
if ctrl<>nil then
If ctrl.Name=aName Then
return ctrl
End If
end if
end if
next
end if

return nil

End Function
[/code]

[code]Select case dept

case 0’ welding
// do the welding array

case 1’ Steel

//do the steel array

end select[/code]

[quote=304817:@Trevor Campbell]Thanks Norman.

That worked ok. So now that I have the control number, can I then use that number to assign text to the label?

I have tried, but there is no option to use control(3).text = “data”[/quote]

You need to cast control(3) to a Label, i.e.

Label(control(3)).text = "data"

When you check the control for the correct name you may also want to test for the correct subclass before you blindly cast:

if controls(i).Name = ctrlName and controls(i) IsA Label then // in Norman's code snippet

A combination of Norman, Jean-Yves Pochez and Urs Geiser I got it working.

[code]dim ctrlName as string = “lbl” + “steel” + “1”
dim found as boolean
for i as integer = 0 to ControlCount() -1
if controls(i).Name = ctrlName then
// FOUND the one we wanted !!!
found = true
end if
next

if not found then
// went through all the controls on the layout & did not find the one of interest
else
// we did find the one with that name
end if[/code]

And then

Label(control(3)).text = "data"

Thanks guys

you can add exit for after the found=true
this will speed up a little bit the search for a window with a lot of controls.

I have been using this code and it works great. However it doesn’t seem to find the a ContainerControl or a CustomContainerControl I have created.

Does any one have any ideas?

ContainerControls have longer names, as you could see with Introspection.

However, their name ends up by the name you gave them in the IDE, so you can do something like :

[code]dim ctrlName as string = “lbl” + “steel” + “1”
dim found as boolean
for i as integer = 0 to ControlCount() -1
if right(controls(i).Name, len(ctrlName)) = ctrlName then
// FOUND the one we wanted !!!
found = true
end if
next

if not found then
// went through all the controls on the layout & did not find the one of interest
else
// we did find the one with that name
end if[/code]

On vb the control have a index
U can create a pool of control ;
and use txtdata(0) to txtdata(x)
simply name the control with the same name.

What you describe is named Control Set in Xojo.

Select the controls you want in the set, click the gear on top of the Inspector, and make them part of the control set.

ContainerControls only have a placeholder in the Controls() array. It takes a bit more work to get to the actual container from that information.

What extra work would that be?

this is a way to get the containercontrols in a given window.
it’s not very efficient because it iterates on all the objects of the application
but it works !
others (I dont remember who but search for containercontrol on this forum, Beatrix if I recall) prone the use of notificationaction and receiver to act what controls are in the widow, then store them on a private list.

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

[code]Public Function ControlOrContainerControl(Extends win As Window, index As Integer) as Object
’ get the nth control or container control in a given window

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©.Graphics Then
Return ContainerControl(iterator.Current)
End
End
Loop
Else
Return c
End
End

End Function
[/code]

I posted a much simpler method based on the one you posted before. Container Controls names are available in a simple iteration, but they have a bunch of stuff before that’s all, so you look for the right part of the name.

No need to complexify more.

Maybe things have changed, but it used to not be worth anything finding the one in Control(), since you can’t do anything with it once you find it.