Stavm
Stavm

Reputation: 8121

in Visual Basic 6.0: creating dynamic control array

I would like to duplicate a control on my form, with a user control, which has the same name with a 'new' added to it. (Label1 ----> newLabel1)

this is my code snippet:

Private Sub CreateLabel(ByRef lblControl As Control)

    'lblControl - this is the control i would like to duplicate


    'The reference to the new created control itself so i can work with it within this sub.
    Dim newControl As Control


    Set newControl = Form1.Controls.Add _
    ("Project.Project1", "new" & lblControl.Name, lblControl.Container)

    newControl.Visible = True

End Sub

It works great if i want to duplicate a control which isn't indexed how ever, i am having trouble duplicating a control which is in an array as lblControl.Name simply take its name, not its index, and replacing the name with an indexed name (lblControl.Name & "(" lblControl.Index & ")" doesn't work really ..

In addition, creating a control and changing its index value after creation is not working.

So my question is, how can i create an array using the above method ?

Upvotes: 4

Views: 3687

Answers (1)

tcarvin
tcarvin

Reputation: 10855

If a control is already a control array, then you use Load to create a new instance of the control.

Assuming you have a label lblControl with an Index set to 0 at design time (making it a control array), you would use the following code to add another instance of it.

Dim newControl As Control
Load lblLabelInControlArray(1) '1 is the Index value that will be sued
Set newControl = lblLabelInControlArray(1)

Obviously you will want to keep track on the Indexes you use, as VB6 does allow gaps so as you Load and Unload them it can get confusing.

Upvotes: 2

Related Questions