• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

Putting controls on form

I need to put some controls on form in Visual Basic at run time. How do I do it?
0
sshayevich
Asked:
sshayevich
  • 4
  • 2
  • 2
  • +2
1 Solution
 
jjmartinCommented:
Add the first control, and give it an index of zero. (i.e. cmdTest(0)

Then, at runtime you can use the load method to load a new control:

Load cmdTest(1)

Then you have to postion it (it is loaded in exactly the same postion as the control with an index of zero) and set the visible property to true in order to see it.
0
 
RuchiCommented:
You need to delcare a variable to hold the reference to the control. Use the add method to create the instance of the control. Set the properties of the control to position it on the form and make it visible to you.

Try something like this...

Private Sub Command1_Click()
Dim otxtnew As TextBox

Set otxtnew = Controls.Add("VB.Textbox", "otxtNew")
With otxtnew
..Visible = True
..Text = "New Control"
..Width = Me.TextWidth(.Text) + 200
..Height = Me.TextHeight(.Text) + 50
..Top = 100
..Left = 300
End With
End Sub
0
 
AzraSoundCommented:
At design time give the control an index of 0 so as to create a control array. Then when you want to load a new instance of it, do it like this:

Textbox for example

Load Text(1)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
AzraSoundCommented:
geez i type slow
0
 
gfaucherCommented:
place all the controls you need on your form when building it an put the visible property at false then at run time change the visible at true for the control you need to use
0
 
AzraSoundCommented:
gfaucher...the question was how to add controls at run time.
0
 
sshayevichAuthor Commented:
When I try to add a new control to controls collection of the form, I don't get a list of properties and methods where I can pull the Add method from. Any idea why?
0
 
AzraSoundCommented:
if youre talking about the convenient popup menus that appear when you normally type in comments like text1.text (when you type the "."), that won't appear when referencing the control collection of the form. However it doesnt mean its not working.  It should still run fine.
0
 
sshayevichAuthor Commented:
Thank you!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now