Dynamically create

I want to dynamically create a 2x2 grid of buttons.

If there’s a way, please let me know.
ex)

dim a(2,2) as DesktopButton
for x as Integer = 0 to 1
  for y as Integer = 0 to 1
    a(x,y) = new DesktopButton
  next
next

Where do you want to put the buttons? So far you created them but haven’t added them to the layout.

I want to place it on a Windows Form.

Hmm. Not sure what a Windows Form is but there are methods for embedding newly created controls onto a layout (such as EmbedWithin) so I hope someone will indicate the most appropriate one for your situation.

please look in the desktopwindow class, you will see 3 methods to add,remove and list controls.
https://documentation.xojo.com/api/user_interface/desktop/desktopwindow.html#methods

you need also a AddHandler for click events.
https://documentation.xojo.com/api/language/addhandler.html

if you create a DesktopButton object at runtime you must set default properties by yourself.

dim a(2,2) as DesktopButton is only valid in method scope, somehow useless.

Windows Form used to be the old Visual Basic name for a Window. Is that what you mean, a Window?

Or place them within a Container?

The issue has been resolved.

Thank you for your attention.

const btnSize = 20
for row as Integer = 0 to 7
  for col as Integer = 0 to 15
    btn(row,col) = new ccBtn
    btn(row,col).EmbedWithin(self,col+100,row*24, 24,24)
    btn(row,col).Left = 180+col*35
    btn(row,col).Top = 73+row*34
    btn(row,col).Width = btnSize
    btn(row,col).Height = btnSize
  next
next
btn(2,3).PushButton1.Caption = "0"
1 Like

Note that having many controls on a window will slow things down, especially if you later expand the number of buttons.

if you do, you may find it faster to emulate buttons by drawing button shapes on the window, and detecting mousedown/up events , then dividing the x and y co-ordinate by the ‘button size’ to calculate which ‘virtual button’ was clicked

1 Like

DynaButtons.zip (5.6 KB)

1 Like

Thank You.

1 Like

Just tiny enhancements, fixed few typos, a parameter order changed to make it one parameter less verbose for most cases. Before: (visible, handler), now : (handler, visible)

New DynaButton(…) defaults to invisible
DynaButton.Create(…) defaults to visible

DynaButtons_1.1.zip (5.9 KB)