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?
caraf_gConnect With a Mentor Commented:
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
James_stillmanAuthor Commented:

Cheers Caraf_g didn't really think about that one.
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.