VB6如何return一个字符串值从form2到form 1

VB6 How to return a string value from form2 to form 1

在我的项目中有两种形式: 第一种形式我将其命名为 frmSettings ,我将使用文本框将值保存在 INI 文件中。 第二种形式我将其命名为 frmSelectFolder ,我已经包含在 DirListBox 和 2 个命令按钮中

如上图所示,在设置表单中,我有 8 个文本框和 8 个命令按钮,用于浏览将从 frmSelectFolder 中选择的文件夹路径

如何对所有文本框使用 frmSelectFolder 而无需将每个命令按钮的此表单复制到 return DirlistBox 控件值?

这是辅助 frmSelectFolder 表单的一些示例代码

Option Explicit

Private m_bConfirm          As Boolean

Public Function Init(sPath As String) As Boolean
    Dir1.Path = sPath
    Show vbModal
    If m_bConfirm Then
        sPath = Dir1.Path
        '--- success
        Init = True
    End If
    Unload Me
End Function

Private Sub cmdOk_Click()
    If LenB(Dir1.Path) = 0 Then
        MsgBox "Please select a path!", vbExclamation
        Exit Sub
    End If
    m_bConfirm = True
    Visible = False
End Sub

Private Sub cmdCancel_Click()
    Visible = False
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode <> vbFormCode Then
        Cancel = 1
        Visible = False
    End If
End Sub

下面是如何从主 frmSettings

调用上面的 Init 方法
Option Explicit

Private Sub cmdStartupPath_Click()
    Dim sPath           As String
    Dim oFrmSelector    As New frmSelectFolder
    
    sPath = txtStartupPath.Text
    If oFrmSelector.Init(sPath) Then
        txtStartupPath.Text = sPath
        txtStartupPath.SetFocus
    End If
End Sub

Private Sub cmdDownloadPath_Click()
    Dim sPath           As String
    Dim oFrmSelector    As New frmSelectFolder
    
    sPath = txtDownloadPath.Text
    If oFrmSelector.Init(sPath) Then
        txtDownloadPath.Text = sPath
        txtDownloadPath.SetFocus
    End If
End Sub

这里有一个 link 的完整示例项目供您研究:SelectFolder.zip