Adding Controls at Run-Time

J

Jerry Bodoff

Hi,

With Peter's help I have been able to create a controls
collection. I still have 1 more problem that I cannot
seem to solve.

I create the Controls on the form without any problem and
all works well. I then tried to create the same controls
in a Frame. The controls do not display and I am not
sure why. The code I am using is:

For SeqNo = 1 to Ubound(CmdCaptions)
Set ctlCB = fraTest.Controls.Add _
("Forms.CommandButton.1","cmdButton" & SeqNo)

--- setting properties ---
--- setting next button location ---
--- adding to controls collection ---
Next SeqNo

Can anyone please tell me what I am doing wrong? I am
assuming that run-time controls can be added to a frame.
Any help would be greatly appreciated.

Jerry B.
 
P

Peter Hewett

Hi Jerry Bodoff

I created a Form with a Frame and an CommandButton (which adds a new CommandButton control
to the frame). The following code is working for me:

Private Sub btnAdd_Click()
Dim ctlCB As MSForms.Control

With fraTest
Set ctlCB = fraTest.Controls.Add("Forms.CommandButton.1")
With ctlCB
.Top = 12
.Left = 20
.Caption = "New Button"
End With
End With
End Sub

HTH + Cheers - Peter
 
J

Jerry Bodoff

Hi Peter,

Once again thanks for your help. I put your simplified
code into my form and all worked well. Tried to put the
control into my frame and it went "bump". I finally
found my "dumb" error. I forgot that the control
position is relative to the frame and not the form. Once
I corrected the control positioning all worked fine.
Sometimes you cannot see the forest for the trees.

What is your reason for using "With fraTest" around the
control commands?

Once again thanks.

Jerry B.
-----Original Message-----
Hi Jerry Bodoff

I created a Form with a Frame and an CommandButton
(which adds a new CommandButton control
 
P

Peter Hewett

Hi Jerry Bodoff

None, it was crap code I posted. When I was playing with it in the IDE I was doing other
stuff in the With block. I plain and simply forgot to remove the redundant code before I
posted it!

Cheers - Peter


Hi Peter,

Once again thanks for your help. I put your simplified
code into my form and all worked well. Tried to put the
control into my frame and it went "bump". I finally
found my "dumb" error. I forgot that the control
position is relative to the frame and not the form. Once
I corrected the control positioning all worked fine.
Sometimes you cannot see the forest for the trees.

What is your reason for using "With fraTest" around the
control commands?

Once again thanks.

Jerry B.
(which adds a new CommandButton control

HTH + Cheers - Peter
 
J

Jerry Bodoff

Hi Peter,

Thanks for your reply.

Jerry B.
-----Original Message-----
Hi Jerry Bodoff

None, it was crap code I posted. When I was playing
with it in the IDE I was doing other
stuff in the With block. I plain and simply forgot to
remove the redundant code before I
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top