Creating controls programatically hint for beginners

I’ve had a lot of help recently from many of the Xojo experts, so this is payback that I hope will help beginners trying to create controls programatically. Be careful not to make a couple of mistakes I did. There are other ways of doing it now, but this way suits me fine.

I used a default button (the ones with caption “OK” in the library). They react if you press the return key during the running of the app. Use ordinary buttons if you don’t intend to use this feature. If you use default buttons, you’ll have to leave one around to clone. Make sure you have some code in it such as:

if me.index=0 then
  me.visible= false

If you are going to use these buttons to react to the return key, make sure you only have one that has the default and is visible on your window at a time. It is easy enough. Say you have 10 clones and you want button 3 to be in charge:

for n as integer = 1 to 10
  myButton(n).default = false
  myButton(n).visible = false
myButton(3).default = true
myButton(3).visible = true

If you are going to test features of your app by throwing a test button on your window to run code, give it a caption “Test”, and use an ordinary button, DON’T use a default button

When I was working on cloning controls I put a default button on the window, wrote a method called cloneControls and tested it by making cloneControls the action when the button was pressed. I later incorporated the method at the start of the app but failed to delete the action from my test button or, even safer, to delete the test button.

Fast forward to a fortnight ago, I found a curious problem in that all my controls would suddenly revert to their initial conditions, throwing away all the formatting I had painstakingly added up to that point and I had no idea why.

I was hitting the return key, assuming a clone button with default true on my window would pick it up, but of course the old test button with the createControls action in it was picking it up instead. It has been bugging me ever since until I woke up this morning and realised what I had done. Problem solved. If you haven’t been careful in making sure that something with the focus will deal with a return key click, you may get unpredictable results.

While I am at it, here is a simple cloneControls routine that works, making 10 copies of desktop control sets – myLab, a Desktop Label, myTex, a DesktopTextField, and myBut (yes, ha ha, Bart) a DesktopButton. And by the way, ordinary buttons can have the default as well as default buttons, but they initially have default set to false.

For this to work you simply need to have set up a Label, a TextField and a Button on your window, given them names myLab, myText and myBut and most important, made them the first members of a control set, ie myLab(0), myTex(0) and myBut(0).

The point of my using longIrrelevantName1 etc in the clone routine is that these names are completely irrelevant - after the cloning you will always refer to the clones as myLab(3) myText(6) and myBut(9) etc to access, modify or add to the Properties of the control.

I would imagine this technique would work with your own custom controls, but when I was starting, I had a lot of problems and found I could do very well with the standard controls. Hope this helps.

Var longIrrelevantName1 As myLab
Var longIrrelevantName2 As myTex
Var longIrrelevantName3 As myBut

for n as integer = 1 to 10
  longIrrelevantName1 = new myLab
  longIrrelevantName1.height = 30
  longIrrelevantName1.width = 100
  longIrrelevantName1.left = 100 = 100+n*40
  longIrrelevantName1.FontName = "Arial"
  longIrrelevantName1.FontSize = 11
  longIrrelevantName1.TextColor = color.Black
  longIrrelevantName1.TextAlignment = TextAlignments.Left
  //add other stuff now if you want
  longIrrelevantName2 = new myTex
  longIrrelevantName2.height = 20
  longIrrelevantName2.width = 80
  longIrrelevantName2.left = 200 = 100+n*40
  longIrrelevantName2.BackgroundColor= &c008000EE//light green
  if n = 1 then
    longIrrelevantName2.visible = false
  elseif n>0 then
    longIrrelevantName2.visible = true
  //add other stuff now if you want
  longIrrelevantName3 = new myBut
  longIrrelevantName3.height = 30
  longIrrelevantName3.width = 100
  longIrrelevantName3.left = 300 = 100+n*40
  longIrrelevantName3.default = false
  longIrrelevantName3.visible = true
  //add other stuff now if you want
1 Like