Display an array of controls

Hi.
I’m trying to display an array of checkboxes (though I’d presume the process would work if it were any control).
I have the following property in the window:

chks() As Checkbox

Then, in the windows Open event handler:

[code] Dim i as Integer
Dim y as CheckBox

// create an arbitrary number of checkboxes…for example, 6
for i = 0 to 5
y = new CheckBox
chks.append y
next

// display the checkboxes in the window, one below the next
Dim h As integer = 0
For i = 0 to chks.Ubound
chks(i).Left = 100
chks(i).Height = 23
chks(i).Width = 50
chks(i).Top = h
h = h + chks(i).Height
chks(i).Visible = True
Next[/code]

The app compiles without error, but nothing is shown in the Window…
What am I doing wrong?
Thanks in advance!

the problem is you cannot create a window control by code, but you must duplicate an existing control.
you must create a checkbox in the window, make it not visible within the IDE
then by code, you make a new of the checkbox you created, set it’s visible property to true (and offset it a little)
and they will appear.

Thanks for the prompt reply Jean-Yves!

…but this presents a problem: My intent was to be able to create an arbitrary number of checkboxes.
(so while in this example, I used 6, I’d like it to be able to be any number).

My final goal is to be able to take the rows of a database table, and present them as checkboxes in a scrollable window.

[quote=303885:@Mike Pearson]…but this presents a problem: My intent was to be able to create an arbitrary number of checkboxes.
(so while in this example, I used 6, I’d like it to be able to be any number).[/quote]
He had told you exactly what you need to do :slight_smile:
Let me repeat his instructions for you:

[quote=303884:@Jean-Yves Pochez]you must create a checkbox in the window, make it not visible within the IDE
then by code, you make a new of the checkbox you created, set it’s visible property to true (and offset it a little)
and they will appear.[/quote]

Now, my UX suggestion:

[quote=303885:@Mike Pearson]
My final goal is to be able to take the rows of a database table, and present them as checkboxes in a scrollable window.[/quote]
Use a Listbox, it’ll be easier for your users and you.

Apologies. I thought he meant create instances for each checkbox needed.

However, further apologies in advance…but you’re going to have to walk me through this:

I added a CheckBox control to the Window layout, “CheckBox1”.
Referring to the examples in the Xojo New documentation I then tried:

// create an arbitrary number of checkboxes...for example, 6 Dim y as CheckBox for i = 0 to 5 y = new CheckBox1 chks.append y next

But get a compile error.

I also tried:

for i = 0 to 5 CheckBox1 = new CheckBox chks.append CheckBox1 next

But this also does not compile…

http://developer.xojo.com/userguide/desktop-control-sets

I made an example project for you, but seriously, use a listbox.
controlset.xojo_binary_project

Also, this will only be available until Dropbox kills free Public folders.

Ah, I didn’t realize the control had to be part of a Control Set in order to clone it.

Got it working. To summarize (for any newbies like me), the process is as follows:

  1. Add a control (e.g. Checkbox) to the Window’s layout, and set its visibility to False.
  2. Create a new Control Set and make the added control a member (with Index = 0)
  3. Create a Window Property that is an array of the control type
  4. Populate the array in an event handler or method as follows (in this example, the control is of type CheckBox):

// create an arbitrary number of checkboxes...for example, 6 Dim y as New CheckBox for i as Integer = 0 to 5 y = New CheckBox1 MyControlArray.append y next

  1. When you want to display the controls, do so as follows:

// display the checkboxes in the window, one below the next Dim y As integer = 0 For i As integer = 0 to MyControlArray.Ubound MyControlArray(i).Top = y y = y + MyControlArray(i).Height MyControlArray(i).Visible = True Next

Thanks for your help everyone! Much appreciated.

Oh I like your idea for maintaining the top position, I just manually added the height.
But for real, use a Listbox please.

Why use a Listbox?
The idea is that I can have an array of controls (read from a database), that can then be manipulated and written back to the database.
I need the user to be able to interact with the control. If it’s a Listbox, it has to be just text, no?

…or do Listboxes allow you to insert controls into the cells?

You can have a checkbox column in a listbox. If you’re working with a list of data that Listbox will in most cases be easier for everyone involved. Maintaining your own scrolling control is more difficult.

It sounded like you were presenting the Database columns to the user. If you are doing something else with the data, then it doesn’t apply the same way. There weren’t many details, so I could be off on what your end goal is; and I might be totally wrong.

Thanks for the suggestion Tim!

I like the way that the Listbox automatically add the scrollbar, when the added rows exceed the listbox’s height…
Unless there’s a simple Window setting I’m missing that will auto-add scrollbars when contents/controls are placed outside its specified dimensions, this is definitely a shortcoming of the method I proposed.

It’s a shame, however, that in terms of controls the listbox cell contents are limited to Checkbox controls.
With the non-listbox method (just an array of Controls), the controls can be anything. …for example Popup/Drop-down lists.

you can put almost anything in a listbox cell
see here : https://forum.xojo.com/19393-desktop-listbox-with-combobox

I’ve been trying to use this solution in a web app but nothing is visible. Did I miss something or does this not work in a web app?

My Code:

dim top as integer = 0
dim y as weblabel
for i as integer = 0 to 4
y = new lblTest
labelsArray.append y
next i

for i as integer = 0 to 4
top = top + labelsArray(i).height + 10
labelsArray(i).left = 20
labelsArray(i).top = top
labelsArray(i).enabled = true
labelsArray(i).text = "Test Label " + str(i)
labelsArray(i).visible = true
next i