Referring to Controls in Code

  1. 5 months ago

    Trevor C

    Dec 20 Melbourne, Australia
    Edited 5 months ago by Trevor C

    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.

  2. Norman P

    Dec 20 Xojo Inc

    Check out control sets

  3. Steve K

    Dec 20 Melbourne, Australia

    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.

  4. Trevor C

    Dec 20 Melbourne, Australia

    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?

  5. Norman P

    Dec 20 Xojo Inc

    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://docs.xojo.com/index.php/Window.ControlCount

  6. Trevor C

    Dec 20 Melbourne, Australia

    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"

  7. Jean-Yves P

    Dec 20 Pre-Release Testers, Xojo Pro Europe (France, Besancon)

    use this function to get any control by it's name. then you can set anything in the control by 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
  8. Jeff T

    Dec 20 Midlands of England, Europe
    Select case  dept
    
    case  0' welding
    // do the welding array
    
    case 1' Steel
    
    //do the steel array
    
    end select
  9. @Trevor C 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"

    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
  10. Trevor C

    Dec 22 Melbourne, Australia
    Edited 5 months ago by Trevor C

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

    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

    And then

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

    Thanks guys

  11. Jean-Yves P

    Dec 22 Pre-Release Testers, Xojo Pro Europe (France, Besancon)

    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.

  12. 3 months ago

    Trevor C

    Feb 15 Melbourne, Australia

    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?

  13. Michel B

    Feb 15 Pre-Release Testers, Xojo Pro

    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 :

    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
  14. Massimiliano C

    is not verified Feb 16 Europe (Italy, Bergamo)

    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.

  15. Michel B

    Feb 16 Pre-Release Testers, Xojo Pro

    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.

  16. Tim H

    Feb 16 Pre-Release Testers Portland, OR USA

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

  17. Trevor C

    Feb 16 Melbourne, Australia

    @Tim H 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?

  18. Jean-Yves P

    Feb 16 Pre-Release Testers, Xojo Pro Europe (France, Besancon)

    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.

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

    Feb 17 Pre-Release Testers, Xojo Pro

    @Jean-YvesPochez this is a way to get the containercontrols in a given window.

    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.

  20. Tim H

    Feb 17 Pre-Release Testers Portland, OR USA

    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.

  21. Newer ›

or Sign Up to reply!