例如,假设我想要一个添加ComboBox的方法。也许我试试这个
Public Sub AddComboBox()
Dim cb As MSForms.ComboBox
Set cb = <Calling form module>.Controls.Add("Forms.ComboBox.1")
End Sub
我如何获得<Calling form module>
?
答案 0 :(得分:1)
我认为你写错了。不要试图确定谁调用该方法,而是将<Calling Form Module>
作为参数传递给AddComboBox()
。像这样:
Public Sub CallToAddComboBox()
AddComboBox(<Calling form module>)
End Sub
Public Sub AddComboBox(CallingFormModule as <Module Object Type>)
Dim cb As MSForms.ComboBox
Set cb = CallingFormModule.Controls.Add("Forms.ComboBox.1")
End Sub
答案 1 :(得分:1)
正如其他人所说,将表单实例传递给class方法。与其他人不同,我将添加:
Me
关键字。他是一个简短的例子:
' <Module1.bas>
Option Explicit
Sub Main()
UserForm1.Show vbModeless
UserForm2.Show vbModeless
End Sub
' </Module1.bas>
' <UserForm1.frm>
Option Explicit
Private Sub UserForm_Activate()
Dim c As Class1
Set c = New Class1
c.AddComboBox Me
End Sub
' </UserForm1.frm>
' <UserForm2.frm>
Option Explicit
Private Sub UserForm_Activate()
Dim c As Class1
Set c = New Class1
c.AddComboBox Me
End Sub
' </UserForm2.frm>
' <Class1.cls>
Option Explicit
Public Sub AddComboBox(ByVal MSForms_UserForm As MSForms.UserForm)
Dim cb As MSForms.ComboBox
Set cb = MSForms_UserForm.Controls.Add("Forms.ComboBox.1")
End Sub
' </Class1.cls>