hatman
Well-known Member
- Joined
- Apr 8, 2005
- Messages
- 2,664
I feel like I am missing something here. I use Class Modules to hook events of runtime created controls in design-time created forms. When I copy the resulting architecture to a new project, I wind up dragging a Form (that may be empty) plus a Class Module into the new file. That's fine, but recently I started wondering if I couldn't move the userform to a runtime architecture. What would that look like? This seems like is should work... but it does not. Any advice?
Code:
Private U_Frm As clsRunTime_Form
Sub Runtime_Stuff()
Set U_Frm = New clsRunTime_Form
VBA.UserForms.Add(U_Frm.Frm.Name).Show
Set U_Frm = Nothing
End Sub
Code:
Option Explicit
Public Frm As Object
Private WithEvents Okay_Button As MSForms.CommandButton
Private Sub Class_Initialize()
Const Gap As Integer = 10
Set Frm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
Frm.Properties("Caption").Value = "New Form"
Frm.Properties("Width").Value = 240
Frm.Properties("Height").Value = 210
Frm.Properties("StartupPosition").Value = 2
Set Okay_Button = Frm.Designer.Controls.Add("Forms.commandbutton.1")
Okay_Button.Width = 75
Okay_Button.Height = 25
Okay_Button.Top = Frm.Properties("InsideHeight") - Okay_Button.Height - Gap
Okay_Button.Left = Frm.Properties("InsideWidth") / 2 - Okay_Button.Width / 2
Okay_Button.Font.Size = 12
Okay_Button.Caption = "Okay"
End Sub
Private Sub Class_Terminate()
ThisWorkbook.VBProject.VBComponents.Remove Frm
End Sub
Private Sub Okay_Button_Click()
MsgBox "Pressed"
End Sub