VB6 – in Visual Basic 6.0: Create an array of dynamic controls

I want to copy a control on my form,
using a user control, which has the same name and added “new”.
(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)
< br /> newControl.Visible = True

End Sub

If I want to copy an unindexed control, it works well
However, I cannot copy the array Because lblControl.Name just takes its name, not its index, and replaces the name with the index name (lblControl.Name&”(“lblControl.Index& “)” really does not work..

In addition, creating a control and changing its index value after creation does not work.

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

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

Assuming you will index There is a label lblControl when set to 0 (making it a control array), and you will add another instance of it using the following code.

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 track The index you use, because VB6 does allow gaps, it will be confusing when you load and unload them.

I want to copy a control on my form ,
Use a user control, which has the same name and added “new”.
(Label1 —-> newLabel1)

This is my code snippet:

p>

Private Sub CreateLabel(ByRef lblControl As Control)

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

< br />'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

If I want to copy an unindexed control, it works well
However, I cannot copy the control in the array because lblControl.Name just takes its name, not its index, and uses the index name Replacing the name (lblControl.Name&”(“lblControl.Index& “)” really does not work..

In addition, creating a control and changing its index value after creation does not work.

< p> So my question is, how to use The above method creates an array?

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

Assuming you are designing There is a label lblControl when the index is set to 0 (making it a control array), and you will add another instance of it with the following code.

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 track the index you use, because VB6 does allow Gaps, so it can be confusing when you load and unload them.

Leave a Comment

Your email address will not be published.