您的SelectedPPT
正在打破封装并且可以从表单外部设置,而不仅仅是表单的代码。您可以通过现场Private
和暴露Property Get
程序为它减轻这一设计问题:
Option Explicit
Private SelectedPPT As String
Public Property Get SelectedFile() As String
SelectedFile = SelectedPPT
End Property
设置在窗体的Initialize
或Activate
处理程序ComboBox
行源,所以它的初始化一次。然后在ComboBox中选择更改时指定SelectedPPT
- 这是您的ComboBox1_Change
处理程序。
这消除了对[设置PowerPoint]按钮及其Click
处理程序的需求。
我将有一个[确定]和[取消]按钮,我会做的形式记住无论是取消:
Private IsCancelled As Boolean
Public Property Get Cancelled() As Boolean
Cancelled = IsCancelled
End Property
Private Sub OkButton_Click()
Me.Hide
End Sub
Private Sub CancelButton_Click()
IsCancelled = True
Me.Hide
End Sub
然后你还需要考虑的情况:用户点击红色的X按钮关闭表格;你可以做,通过处理形式QueryClose
事件:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
IsCancelled = True
Cancel = True
Me.Hide
End If
End Sub
逻辑的其余部分属于调用代码 - 假设的形式被称为MyAwesomeForm
;你有这样的事情:
Dim filename As String
With New MyAwesomeForm
.Show
If Not .Cancelled Then
filename = .SelectedFile
'do whatever you wanted to do with that filename
End If
End With
注:
- 在任何时候调用的形式
Unload Me
- 这是调用者的责任,创造和破坏形式对象
- 来电每次使用表格的
New
实例
- 调用者具有对任何值的只读访问权限for通过属性公开
'功能PassPPT(strSelectedPPT作为字符串)......结束功能'使用像'PassPPT(me.combobox1.value)' –