0

我有一个用户表单设置来收集用户在创建带有子文件夹和文件的目录时的输入。输入是通过使用 Treeview 进行的,并且文件结构是通过默认选择 Checked on 或 off 预先确定的。用户可以切换这些子文件夹和文件的创建以满足他们的需要。我添加了“全选”和“全部清除”的选项,这可以通过 Treeview 节点轻松完成。我还想为用户提供恢复默认选择的选项。为此,我添加了一个“默认选择”命令按钮,用于卸载用户窗体,然后加载一个新实例。

在测试期间,我注意到我的代码中有一个奇怪的错误。会弹出一个 Msgbox,告诉我脚本已经运行,没有任何错误。但是,对于用户单击“默认选择”的每个实例,Msgbox 都会弹出很多次。例如,如果用户点击“默认选择”3 次(我不知道他们为什么会这样做,除了点击快乐和容易发生意外......),Msgbox 弹出 4 次;一次成功测试,每次单击按钮时再增加 3 次。

这是将用户窗体代码精简为一个 CommandButton cmdDefaultSelect

Option Explicit

Private Sub cmdDefaultSelect_Click()
    
    'Reset default node checked values by reloading form
    Unload Me
    Call TestUnloadUserform

End Sub

...这是具有相同症状的测试模块:

Option Explicit

Public Sub TestUnloadUserform()
    
    Dim frM                             As frmTest
    
    Set frM = SetupTestFrm()
    
    frM.Show vbModal
    
    'Unload userform if it's already loaded
        'This sub first loads the form
        'Once the form is unloaded by cmdDefaultSelect_Click,
        'the script continues to run from here, immediately
        'after frM.Show
        'If it's not unloaded here, then there is usually an error
    If Not frM Is Nothing Then
        Set frM = Nothing
    End If
    
    MsgBox _
        Prompt:="Test complete.", _
        Buttons:=vbOKOnly + vbInformation, _
        Title:="Great Job"
    
End Sub

Public Function SetupTestFrm() As frmTest
    
    Set SetupTestFrm = New frmTest
    
    'In actual form, this is where the Treeview and Node properties are set
    
End Function

对于初稿,它有效。此示例中不包含额外的代码,但子文件夹和文件的创建没有错误。这也是我能想出的最简单的解决方案,从本质上重绘 Treeview。但是,我知道我没有正确加载/卸载我的用户窗体,并且我可能会在稍后添加更多功能时遇到麻烦。我不希望每次卸载 Userform 时脚本都结束,但我不确定如何在 Userform 和标准模块之间构建代码。

4

0 回答 0