Steven de Beer
Steven de Beer

Reputation: 138

Pass value back to variable based form

I know I might be over complicating things but I require some way of sending a test string back to another form referenced by a Form object

below is some detail:

Form calling 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 is declared below on the SearcherForm

Public ReturnForm As Form

the SearcherForm detail is not important, only the string it should return. below is the return method i have

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

Lastly my Disposed proc

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

Any Ideas?

Upvotes: 1

Views: 59

Answers (2)

Fabio
Fabio

Reputation: 32445

My suggestion use Form.ShowDialog() and returned value.
In that way both forms stay loosely coupled and SearcherForm not needed all the time stay in the memory

Searcher form:

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

Main Form

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

By Form.ShowDialog() form shown in modal state, so your main form will be not accessible during Searcher form is shown - this exactly what you tried to do by Me.Enabled = False

Upvotes: 1

Steven de Beer
Steven de Beer

Reputation: 138

Never mind I thought about some logic to work with what I know works.

I removed the ReturnSelection proc in the SearcherForm and added a trigger on the returning form to read the variables in the SearcherForm based on the same concept (just inverted so to speak). See below:

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

I hope this helps someone else. Any feedback is still welcome.

Upvotes: 0

Related Questions