Cloning Dynamically Controls

I want to clone a set of (two) controls (Label and TextField) and I have trouble doing that.

In “UserGuide-UserInterface.pdf”, page 108 explain how to do that and share the example below:

Dim pb As PushButton pb = New PushButton1 //create clone pb.Caption = "Clone" pb.Left = Me.Left + Me.Width + 10

That works fine if you really read the page and do as explained.

BUT: I want to be able to dynamically set the number of clones (as part of a larger project). To simulate what I want to do, I used a Loop:

[code]Dim pb As PushButton
Dim i As Integer

for i = 0 to 5
pb = New PushButton1 //create clone
pb.Caption = "Clone " + Str(i)
pb.Left = Me.Left + Me.Width + 10
the code compiles, but…

a. I only saw one clone (the others are certainly below this one),
b. how do I call these to change the caption of the Label (in my code) and the contents of the TextField (still in my code) ?

The same example can be found in new.

BTW: some people will want too see my code, so here it is:

[code]Sub Action(index as Integer)
Dim tf As TextField
Dim L As Label
Dim LoopIdx As Integer

For LoopIdx = 0 to 5
// Create a TextField clone
tf = New TF_Default

// Create a Label clone
L = New L_Default


// Done !
Beep // There is a breakpoint here
End Sub[/code]

In the debugger, I can see that all 6 clones have been created (6 Labels, 6 TextFields).

The question is: how do I set each Label a different Text and the TextFields a different contents; and later, before closing the window, how do I get back the TextFields contents ?

you may need to keep an array with the objects, so they don’t disappear to early?

pb is the clone of the first pushbutton1 so it will have the same top and left coordinate.
you move to the left but every time to the same position
so the clones are one over the other


to set and read the “clone” text field value:

Christian, Antonio: thanks a lot.

here’s the code that works:

[code] Dim tf() As TextField
Dim L() As Label
Dim Cnt As Integer
Dim LoopIdx As Integer

// Set Cnt value (# of clones to create, 9-Based)
Cnt = wMain.LB.ColumnCount

// Set the number of Cells in the Array
Redim tf(Cnt)
Redim L(Cnt)

// Fill Cell 0 with the original TextField
tf(0) = TF_Default(0)
L(0) = L_Default(0)

// Create the clones
For LoopIdx = 1 to Cnt

// Clone a Label, set its location and feed it !
L(LoopIdx) = New L_Default // create a Label clone
L(LoopIdx).Top = L(LoopIdx-1).Top + L(LoopIdx-1).Height + 10
L(LoopIdx).Text = wMain.LB.Heading(LoopIdx-1)

// Clone a TextField, set its location and feed it !
tf(LoopIdx) = New TF_Default // create a TextField clone
tf(LoopIdx).Top = tf(LoopIdx-1).Top + tf(LoopIdx-1).Height + 10
tf(LoopIdx).Text = wMain.LB.Cell(0,LoopIdx-1)


// That’s All Folks ![/code]

To make the code working, you need another window (names wMain) an ListBox (LB) in that window - plus a Label (L_Default) and a TextField (TF_Default) in the window where the code resides. AND, the ListBox have to have some filled Rows to make the whole stuff working.

It leaves my original Lable and TextField empty and visible, so I have to work a little bit more. I also have to work a little bit to automate multiple columns if needed, but the bases are here.