首页 > 解决方案 > VB6如何将一个字符串值从form2返回到form 1

问题描述

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

在此处输入图像描述

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

如何为所有文本框使用 frmSelectFolder 而无需为每个命令按钮复制此表单以返回 DirlistBox 控件值?

标签: vb6

解决方案


这是一些辅助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

以下是如何Init从主要调用上述方法frmSettings

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

这是一个完整示例项目的链接,供您研究:SelectFolder.zip


推荐阅读