p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Excel VBA (http://p2p.wrox.com/forumdisplay.php?f=79)
-   -   Class modules (http://p2p.wrox.com/showthread.php?t=32598)

lallemantdo August 17th, 2005 04:15 PM

Class modules
Can you please help me.

I would like to add a control to a form at runtime and be able to assign an event procedure to the newly created control. This would enable me to click on the control to call the procedure.

ie: Have a Commandbutton added to a form at runtime and be able to click on this Commandbutton to call an event procedure.

Thanks for your help.

mjppaba August 19th, 2005 02:58 AM

I would set the form up with the command button from the outset, only have the visible property set to false. When you need the button change this property to True and back to false when finished.



lallemantdo August 21st, 2005 03:30 PM

Thank you for your suggestion.

gcianfanelli August 24th, 2005 02:59 AM

to truly add a control at runtime in VBA you need to execute the Add method of the UserForm object's Controls property. This method expects the ProgID of the control you want to add - something like the following code should be added to your test UserForm:


Option Explicit

Private WithEvents mRuntimeButton As MSForms.CommandButton

Private Sub mRuntimeButton_Click()

  MsgBox "runtime button clicked"

End Sub

Private Sub UserForm_Initialize()

  Set mRuntimeButton = Me.Controls.Add("Forms.CommandButton.1", "mRuntimeButton", True)

End Sub

You'll additionally need to set a whole bunch of properties (Height, Width, Caption, etc) in order to get the button looking as you want it

If you need to know more, highlight the Add method in your VBA environment and hit F1 for some help

lallemantdo August 24th, 2005 08:35 PM

Thank you this is really good stuff!

Never be afraid to try something new.
Remember: Amateurs built the ark. Professionals built the Titanic

All times are GMT -4. The time now is 01:29 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.