Creating a textbox array

How do I create an array of textboxes to make addressing easier.

I think I have been asking such questions on the issues forum by mistake. Apologies for this.

Something along these lines?

Var  textBoxes() as DesktopTextbox, tb as DesktopTextbox

tb = new DesktopTextbox
textBoxes.Add (tb)

Hi Tim
Thanks for your help.
I used this code on a form opening event but got errors. What am I missing.

What does the documentation say? Code posted here may well be untested.

Other very useful information that will help us to help you:

  1. The error message you received
  2. The exact code you put into the Opening event

Code
Var textBoxes() as DesktopTextbox, tb as DesktopTextbox
tb = new DesktopTextbox
textBoxes.Add (tb)

Errors
Window1.Opening, line 1
Can’t find a type with this name
Var textBoxes() as DesktopTextbox, tb as DesktopTextbox

Window1.Opening, line 1
Can’t find a type with this name
Var textBoxes() as DesktopTextbox, tb as DesktopTextbox

Window1.Opening, line 3
Can’t find a type with this name
tb = new DesktopTextbox

So - did you want DesktopTextArea or DesktopTextField. Amend code as appropriate.

I just want an array of textboxes. Obviously your example tries to set up an array of textboxes named tb
I have tried CHATgpt which comes up with solutions that dont seem to match with this latest version.
I have not found any detailed documention of Xojo, only high level stuff.

You do have to name the array something and store it somewhere.

What exactly are you trying to do?
Describe your desired result to us without technical terms.

No, it sets up an arrary of textboxes called textBoxes. Except that Xojo has no such item. It has DesktopTextArea or DesktopTextField, or possibly DesktopLabel. We still don’t know what you want these for and therefore can’t advise as to which you need.

Documentation is to be found at:

https://documentation.xojo.com/

I currently have 12 textboxes which would be easier to manage if they were in an array, rather than separate textboxes. If I could convert them to an array that would be good but if not I can start again with a new array of textboxes. I just need to crack how arrays of controls like textboxes are created. I have not been able to find any decent documentation or youtube on the subject of control arrays that simply provides a technical answer for Xojo.

See if this code gives better results:

Var textBoxes() As DesktopTextArea
Var tb As DesktopTextArea
tb = New DesktopTextArea
textBoxes.Add (tb)

It runs but no textboxes get created as no positions or sizes are stated, I assume.
Moving forward, for which I am grateful but I am surprised that no one seems to have created such beasts before. More ideas welcome.

Xojo doesn’t have textboxes. It has the controls I listed earlier, DesktopTextArea, DesktopTextField, and DesktopLabel. Now, which of these do you have?

Do you already have DesktopTextAreas in the layout or do you want the code to create and place DesktopTextAreas in the layout during runtime?

This brings up a gap in my knowledge:
Can controls be created and placed in a layout by code at runtime?
and/or
Can controls already existing in a layout be duplicated and placed by code at runtime?

Yes to both.

1 Like

You asked about create an array with controls.
It looks like now you don’t know how to use the controls from that array.

As Tim said:

Hi Tim
Apologies for my loose language referring to box controls rather than field controls,
I have 15 Text Fields on my form that I wish to use in an array. This would save some manual insertion of values when developing the program.

Having created them they need to be added to the layout. Then their sizes and positions need to be specified. Use:

myWindow.AddControl (tb)

See the doc for DesktopWindow.

OK. Here’s a start then:

Var textFields() As DesktopTextField, tf As DesktopTextField

tf = New DesktopTextField   // Repeat these as often as
textFields.Add (tf)         // needed to fill your array with the textfields

myWindow.AddControl (tf)
tf.Left   = ??   // Replace the various ?? with the desired values
tf.Top    = ??
tf.Width  = ??
tf.Height = ??

That will give you the control on the window where you want it.