Problem creating desktop controls programatically

I’ve tried this again and again but I can’t get it to work programatically, despite everything I read saying it is easy to do in Xojo.

All I want to do is put a DesktopRectangle, aRect, and two DesktopLabels, lbA and lbB, on the main window and make them control sets, so they become aRect(0), lbA(0) and lbB(0) then programatically use a loop to create 255 clones of each, so I can put them wherever I want and change their attributes by referring to them using their indices. I don’t want to get involved with Containers etc.

I can do exactly what I want if I create 768 controls and dump them on the window in advance, but it is a huge mess and it takes a long time to add or move buttons and checkboxes later on. So I’d be very grateful if anyone can tell explain how to do this. Thanks - Steve

Have you looked at the ControlSets example under Desktop > Controls? It gives a good overview of managing ControlSets at runtime.

I’m sorry but I just don’t understand the code. I’m probably very old-fashioned but I put my code in a method so I can follow it through and there aren’t any methods in this example. I can’t follow what is going on.

Drag a DesktopRectangle to you window.
Using the Inspector change the name to aRect
image

Using the Gear icon, create a New Control Set:
image

Do the same for lbA and lbB and you will end with:
image

Review the code from the Example in ControCountPopup - SelectionChanged to see what code do you need to loop for 255 clones.

OK , Alberto. I knew how to set up the control set but not the rest, and the code you referred me to in ControlCountPopup seems to be doing the trick at the moment.

I did try going down a similar route to this before but found I couldn’t create a method to change any of the attributes of the cloned objects. With this code I can certainly change fillColor and text so that looks very hopeful.

I will now try to substitute the cloning method for my messy window with hundreds of uncloned controls on it. Many thanks. - Steve

I intrude, even though I may not have understood the problem well. Here is a small piece of code, just to give the idea (parts are missing to work)

  redim listaLabels(-1)
  
  dim coordV as Integer = 0
  dim coordO as Integer = 0

  for k as Integer = 0 to nrElementi
    dim kLab as new Label
    kLab.Width = myWidth
    kLab.Height = myHeight
    kLab.Value = myText
    listaLabels.Append(kLab)
    kLab.EmbedWithin(largeRect, coordO, coordV, kLab.Width, kLab .Height)
    
    coordO = coordO + kLab .Width
    if coordO+kLab .Width > largeRect.Width then
      'andiamo a capo
      coordV = coordV + kLab.Height
      coordO = 0
    else
      'continuiamo sulla stessa linea
    end if
  next

I use runtime control creation a lot

Thanks for all the suggestions. I have now been kicking this around for a couple of days and everything seems to be working perfectly so I have moved over to using runtime controls completely. It’s a relief to get rid of the clutter on the opening window. Thanks again - Steve

1 Like