Referring to Controls in Code

  1. ‹ Older
  2. 4 months ago

    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
  3. 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
  4. @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
  5. Trevor C

    Dec 22 Melbourne, Australia
    Edited 4 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

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

  7. 2 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?

  8. 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
  9. 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.

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

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

  12. 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?

  13. 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
  14. 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.

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

  16. Michel B

    Feb 17 Pre-Release Testers, Xojo Pro

    I use that in RubberViews, and it works perfectly to resize ContainerControls.

  17. Tim H

    Feb 17 Pre-Release Testers Portland, OR USA

    Brilliant! Are you able to access the contents as well?

  18. Michel B

    Feb 17 Pre-Release Testers, Xojo Pro
    Edited 2 months ago by Michel B

    I don't modify the content, since RubberViews manages ContainerControl like windows, and scales their content from the inside.

  19. Tim H

    Feb 17 Pre-Release Testers Portland, OR USA

    I see. So perhaps this is still of limited use.

  20. Michel B

    Feb 17 Pre-Release Testers, Xojo Pro
    Edited 2 months ago by Michel B

    It seems quite possible that the limitations in accessing the content be related to the need to properly cast the control. I am not sure which one of ContainerControl or EmbeddedWindow would work.

  21. Michel B

    Feb 17 Pre-Release Testers, Xojo Pro

    As I suspected, I found no way to correctly cast a control to access the content of a Container Control.

or Sign Up to reply!