在类中为Form动态增加的控件不响应事件,怎么解决?
在类中对Form作一些控件上的增减,并需要增加的控件都响应事件
''Class1.cls''定义Private frm As FormPrivate WithEvents lblAdvance As LabelPublic Sub InitData() Set frm = Form1 Set lblAdvance = frm.Controls.Add("VB.Label", "lblAdvance") lblAdvance.Move 4590, 1950, 780, 195 lblAdvance.BackStyle = 0 'Transport lblAdvance.ForeColor = vbBlue lblAdvance.FontUnderline = True lblAdvance.MousePointer = vbCustom lblAdvance.MouseIcon = LoadResPicture(101, vbResCursor) ''光标也没发生变化哦 lblAdvance.Caption = "高级模式" lblAdvance.Visible = TrueEnd Sub''Label 单击事件Private Sub lblAdvance_Click() MsgBox "Click"End Sub
'Form1Option ExplicitPrivate c1 As Project1.Class1Private Sub Form_Load() Set c1 = New Class1 c1.InitDataEnd SubPrivate Sub Form_Unload(Cancel As Integer) Set c1 = NothingEnd Sub
[解决办法]
'Form1Private Sub Form_Load() Debug.Print Now, "Form_Load()", Hex(Me.hWnd) '''End Sub
[解决办法]
要控件响应事件,必须制定他的容器:
将
Set lblAdvance = frm.Controls.Add("VB.Label", "lblAdvance")
改为
Set lblAdvance = frm.Controls.Add("VB.Label", "lblAdvance",frm)
这样就响应时间了。