Is there a way to create an array of controls. In particular I’d like to create a collection/array of label controls that can be referenced via an index number.
Thanks,
Ken
Is there a way to create an array of controls. In particular I’d like to create a collection/array of label controls that can be referenced via an index number.
Thanks,
Ken
Hello,
Could you expand your use case a bit further? It can surely be done in multiple ways.
For instance, if it is just a handful of Labels, you can add them to a form, which makes it easier to format them individually as you like.
Then, inside function Main(), just detach them from the form and add to your array. It will be executed when your app is initialized.
When you want to access and show them, you can do so by the array index and attach it to the control you want to be its parent.
Detach: Node: removeChild() method - Web APIs | MDN
Attach:
Hope this helps
Ricardo Carraretto
Here’s an old blog post showing how to create an array of buttons. It would work for labels, too.
I’d love to see a new feature added to AppStudio that would support adding an array of controls to a form and could be done using standard VBScript notation (like the experimental ChatGPT feature says):
Dim buttonArray(5) ' Declare an array for 5 buttons
' Assuming you have a form object named Form1
Set Form1 = CreateObject("Forms.UserForm.1")
' Loop to create buttons and add them to the form
For i = 0 To 4
Set buttonArray(i) = Form1.Controls.Add("Forms.CommandButton.1", "Button" & i)
buttonArray(i).Caption = "Button " & (i + 1)
buttonArray(i).Left = 10 ' Set position
buttonArray(i).Top = 10 + (i * 30) ' Space them vertically
Next
' Show the form
Form1.Show
Then I could manipulate the controls in code - like simulate sequential highlighting by referencing their index numbers as an expression.
You could modify the code in the blog post to do something similar:
Function btnMake10Buttons_onclick()
'To make 9 text boxes, change type=button to type=text
Dim i, btn, s
For i=1 To 9
btn=document.createElement("div")
s="<input id='btn" & i & "' type=button "
s+="style='position:absolute; "
s+="width:24px; height:24px; top:360px; "
s+="left:" & (243+i*30) & "px;' "
s+="value=" & i & ">"
btn.innerHTML=s
Form1.appendChild(btn)
buttonArray[i] = btn ' <--- Add this line
Next
End Function
You can then use buttonArray[n] to refer to any of the buttons.
I have complete faith that your suggested code will work - but I don’t want to be writing HTML/CSS (not sure which this is) programs. I want to create dynamic controls using the same terminology that I use to create design-time controls.
Here’s a StackOverflow discussion which does something similar: