Hi,
In vb if i do Controls.Add, that works fine except that if i want to make an array of controls then it doesnt work, how can i create an array like btnOk(1)
Printable View
Hi,
In vb if i do Controls.Add, that works fine except that if i want to make an array of controls then it doesnt work, how can i create an array like btnOk(1)
See if this works. (I can't test it because I don't have VB6)
Code:Private Sub Command1_Click()
Controls.Add "VB.Label", "Label1"
Me!Label1.Move 0, 0
Me!Label1.Caption = "Label1"
Me!Label1.Visible = True
Me!Label1.Index = 0
'Now add more controls with the same name.
Controls.Add "VB.Label", "Label1"
Controls.Add "VB.Label", "Label1"
End Sub
In fact you can't create control arrays at runtime.
Use the load() statement to load a control in an array.
code:
Private Sub Command1_Click()
dim iLoop as Integer
Controls.Add "VBLabel", "Label1"
Label1.index = 0
'Creates an array of 20 controls.
for iLoop = 1 to 1
Load Label1(iLoop)
next iLoop
End sub
Wak, in your coding you have "Label1.index = 0". This will cause an error and your application will not compile, because there is no Label1 yet.
The only way you can do this is by adding one control during design time, then you can load as many as you want during runtime.
Add the control you need, set the index property to 0, thus making it an array, and then you can load instances as you need during runtime.
Hope this helps.Code:'create 2 more textboxes
Load txtTest(1)
Load txtTest(2)
'use the next index in line
Load lblPrompt(lblPrompt.UBound + 1)
Shrog
Ok, i can add controls at design time, do u think that is the best way (using the load statement) ?
I don't know if it's the best way, but it's the best way I know. I use this all the time, and we use it in the commercial software that my company produce.
Shrog