将值传递回基于变量的表单

时间:2016-02-04 09:45:19

标签: vb.net winforms variables visual-studio-2013

我知道我可能会使事情变得复杂但我需要某种方法将测试字符串发送回Form对象引用的另一个表单

下面是一些细节:

表格调用proc

Private Sub VCodeFindBtn_Click(sender As Object, e As EventArgs) Handles VCodeFindBtn.Click
    LSVehicleCodeTxt.Text = ""
    SearchMethod = "V"
    Searcher()
End Sub

Sub Searcher()
    Me.Enabled = False
    SearcherForm.SearchMeth = SearchMethod
    SearcherForm.ReturnForm = Me
    SearcherForm.Show()
End Sub

ReturnForm在SearcherForm上声明如下

Public ReturnForm As Form

SearcherForm的细节并不重要,只有它应该返回的字符串。 以下是我有的退货方法

Sub ReturnSelection()
    Select Case SearchMeth
        Case "V"
            'code needed here to populate Text control (or variable would also work)
            'i thought using ReturnForm.TextBox1.Text=ReturnString or 
            'ReturnForm.ReturnedValue=SearchResult would work
        Case "D"

        Case "V1"

        Case "V2"
    End Select
    Me.Dispose()
End Sub

最后我的Disposed proc

Private Sub SearcherForm_Disposed(sender As Object, e As EventArgs) Handles Me.Disposed
    ReturnForm.Enabled = True
    ReturnForm.Focus()
End Sub

任何想法?

2 个答案:

答案 0 :(得分:1)

我的建议使用Form.ShowDialog()并返回值 这样,两种形式保持松散耦合,并且SearcherForm不需要一直留在记忆中

搜索者表格:

Public Class Searcher
    Inherits Form

    Public Property ReturnValue As String

    Sub ReturnSelection()
        'Make your selection
        'Assign selected value
        Me.ReturnValue = somSelection
        Me.DialogResult = DialogResult.OK 'This will close form automatically
    End Sub

End Class

主要表格

Public Class MainForm
    Inherits Form

    Sub Searcher()           
        Using tempform As New Searcher()
            If tempform.ShowDialog() = DialogResult.OK Then
                LSVehicleCodeTxt.Text = tempform.ReturnValue
            End If
        End Using
    End Sub

End Class

以模态状态显示的Form.ShowDialog()表单,因此在显示Searcher表单时无法访问您的主表单 - 这正是您Me.Enabled = False

所尝试的内容

答案 1 :(得分:0)

没关系 我想到了一些逻辑来处理我所知道的工作。

我删除了SearcherForm中的ReturnSelection proc  并在返回的表单上添加了一个触发器,以基于相同的概念读取SearcherForm中的变量(只是反过来说)。见下文:

Private Sub CreateLogSheetForm_EnabledChanged(sender As Object, e As EventArgs) Handles Me.EnabledChanged
    If Me.Enabled = True Then
        MsgBox(SearcherForm.SearchMeth & vbNewLine & SearcherForm.ReturnString)
        If SearcherForm.SearchMeth <> "" Then
            Select Case SearcherForm.SearchMeth
                Case "V"
                    LSVehicleCodeTxt.Text = SearcherForm.ReturnString
                Case "D"
                    LSDriverCodeTxt.Text = SearcherForm.ReturnString
                Case "V1"
                    LSVAssist1CodeTxt.Text = SearcherForm.ReturnString
                Case "V2"
                    LSVAssist2CodeTxt.Text = SearcherForm.ReturnString
            End Select
            SearcherForm.SearchMeth = ""
            SearcherForm.ReturnString = ""
        End If
    End If
End Sub

我希望这有助于其他人。 任何反馈仍然是受欢迎的。