How to use a new keyword to create a new object in VB6 like a new button, label, etc.

I have a program that takes multiple names and stores them in a file. I want to create a new object (button, label, etc.) for each person saved in the file. I am using this code, but I got the error:

Dim i as new object
set i= new button

      

Error I got: ActiveX component cannot create object

+3


source to share


1 answer


In your form, add a shortcut and a command button. I assume you have already done this.

Select the label and inside the properties window set lblPerson

to property Name

and 0

to property Index

.

Select the button and in the properties window set cmdPerson

to the property Name

and 0

to the property Index

.

You now have two control arrays that you can dynamically set at runtime:

Public Sub AddPersonListControls(idx As Long)
    Load cmdPerson(idx)
    cmdPerson(idx).Caption = "Details"
    cmdPerson(idx).Visible = True
    cmdPerson(idx).Top = cmdPerson(idx - 1).Top + cmdPerson(idx - 1).Height + 10
    cmdPerson(idx).Left = cmdPerson(0).Left

    Load lblPerson(idx)
    lblPerson(idx).Caption = "Person Name " & idx
    lblPerson(idx).Visible = True
    lblPerson(idx).Top = lblPerson(idx - 1).Top + lblPerson(idx - 1).Height + 10
    lblPerson(idx).Left = lblPerson(0).Left
End Sub

      

Wherever you load your face data, create the appropriate controls:



Dim numPersons As Long 
numPersons = 3 ' just an example
Dim i As Long
For i = 1 To numPersons - 1
    AddPersonListControls i
Next

      

Your form should look like this (Note: Form1.ScaleMode

set to 3-Pixels

):

Dynamically loaded controls

Explanation:

While my answer does not directly address your question with a keyword New

, it does show the correct method for dynamically adding new controls in case you don't know how much they should be, thus avoiding using Named Controls

and using instead Array Controls

.

+2


source







All Articles