Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Controls on the fly?

Status
Not open for further replies.
Yes, you can add conrols dynamically at runtime. Call the Add method on the form's control collection and pass in the ProgID of the control that you want to create as well as the control name. You will add a new control to the form and you will receive a reference to it so that you can position it and make it visible.
If you want to trap the events off of the newly created control, you will need to monitor the event queue for the control or write a class wrapper with an internal reference to the control declared withevents. You would set that reference to whatever was returned by the add method of the form's control collection. - Jeff Marler B-)
 
Jeff -

Doesn't an example of the control already have to exist on the form for you to be able to add new instances?

Chip H.
 
Chip,
I used to think that myself as well, but I have since found out that you can use the forms control collection as a control factory. YOu will not automatically get an event sink set up however which is where the class wrapper comes in. You will however need the control referenced in order to get the events off of it in the class wrapper. - Jeff Marler B-)
 
Wow.. Thanks Jeff.. but this is going a little over my head for what I would need to do to make this happen. What I'm trying to do is read an array, I would need as many CheckBoxs as the UBound of that array. (to Dynamicly create parts of a Form due to it working with a database)

I 'understand' the concepts of what you are speaking of, but no idea on how to Implament it. Would you know of a website or something I can view to help me figure this out? ---===///The PogoWolf\\\===---
Darkness..Bleakness..Plastic forks..?

 
I too need to dynamically create controls in my application. You can try this code.

For i = 1 To rs.RecordCount
Load optCase(i)
With optCase(i)
.Caption = rs!filed_name
.Height = 350
.Width = 1935
.Left = 4600
.Top = 850 + (i * 300)
.Visible = True 'this is important bec. your template is invisible at design time.
rs.MoveNext
End With
Next
With this code, you need to have a template of the control you want to add, in this case option(0). The .properties just sets the position of each new controls on the form.

You can also add controls at run time without a template,
here's a code you can try:

Set optButtons = Controls.Add"VB.OptionButton",name)

 
The last pasrt of EdRev's post show the correct way to use the form's control collection to add forms . . . remember however that using ONLY this method, you will not get any of the events fired off of the control (try adding a command button with this method and then see if it does anything). That is where you need the classwrapper . . . - Jeff Marler B-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top