Adding items to a frame on the fly

I want to add items to a frame in runtime.

During runtime I am going to create the labels and load them into a pre existing frame if I can. iS There a way that I can do this?

Can anyone help please? I'm sure it should be easy if you know how.

I have an API call setParent but cannot use it because the label control has no Hwnd property and this is required by the API call.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Example. Create a form. place a label on it. Set its index property to 0
Place a frame on it.

The code below loads label1(1), and puts it in the frame. The second line is the important one.

Load Label1(1)
Set Label1(1).Container = Frame1
Label1(1).Top = Screen.TwipsPerPixelY
Label1(1).Left = Screen.TwipsPerPixelX
Label1(1).Caption = "Hello World"
Label1(1).Visible = True

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
James_stillmanAuthor Commented:

Cheers Caraf_g didn't really think about that one.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Just thought I'd add my $0.02....

I tried the code above for labels and for textboxes.  The labels worked fine, but the textboxes caused big fat memory access errors.  They were so bad that I ended up having to totally un-install and then re-install VB6!

Here's the VB6 code I ended up using...

Dim mytextbox() as textbox
ReDim Preserve mytextbox(0)
dim strTemp as String
dim xpos as Long
dim ypos as long

xpos = Frame1.Left + 700 ' change as needed
ypos = Frame1.Top + 700 'change as needed

For i = 0 to end
  ReDim Preserve mytextbox(ubound (mytextbox) + 1)
  strTemp = "TextBox" & i
  Set mytextbox(i) = Controls.Add("VB.TextBox", strTemp)
  With mytextbox(i)
    Set .Container = Frame1
    .Top = ypos
    .Left = xpos
    .Visible = true
  End With
  ypos = ypos + 700

So now you know. :)
Blimey, this question is so old I had forgotten I ever answered it.

I assume these errors happened when the form was unloaded?
Here's an example that works fine:

Put a text box on the form and give it index 0

Put a frame on the form

Put a button on the form

Each button click loads a new instance of the text box.

On the form_unload procedure the textboxes are unloaded carefully.

This code runs without problems on my PC - VB6+SP4

Option Explicit

Private Sub Command1_Click()

Load Text1(Text1.UBound + 1)

With Text1(Text1.UBound)
    Set .Container = Frame1
    .Top = Text1(0).Height * (Text1.UBound - 1)
    .Visible = True
End With

End Sub

Private Sub Form_Unload(Cancel As Integer)

Do While Text1.UBound > 0
    Set Text1(Text1.UBound).Container = Me
    Unload Text1(Text1.UBound)

End Sub
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.