reactor
reactor

Reputation: 1931

how to manage pass value set by dialog box in VB form application to its caller

I have a form Main that calls open a dialog, which gets a text value response and that value would need to be used in Main. I was wondering how to achieve this where Main would have access to this after the dialog closes. The minimal example of this is as follows:

Private Class Main
    Private Sub btn_Click(sender As Object, e As EventArgs) Handles btnOpenDialog.Click
        Dim dialog As New Dialog
        dialog.Show()

    End Sub
End Class

and Dialog

Private Class Dialog
    Private response As String
    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        response = txtResponse.Text
        Me.Close()
    End Sub

End Class

Upvotes: 0

Views: 225

Answers (2)

Alfredo J. G. R.
Alfredo J. G. R.

Reputation: 29

In that case the simple solution could be the following:

Private Class Main
    Private Sub btnOpenDialog_Click(sender As Object, e As EventArgs) Handles btnOpenDialog.Click
        Dim AskDialog As New Dialog
        Dim Response As String
        Response = AskDialog.DialogShow()
    End Sub
End Class

and Dialog

Public Class Dialog
    Private response As String
    Public Function DialogShow() As String
        Me.ShowDialog()
        Return response
    End Function

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        response = txtResponse.Text
        Me.Close()
    End Sub
End Class

Upvotes: -1

jmcilhinney
jmcilhinney

Reputation: 54417

Just provide a pass-through property for the Text of the TextBox:

Public ReadOnly Property Response As String
    Get
        Return txtResponse.Text
    End Get
End Property

It's hard to know for certain but you probably out to be displaying the form as a modal dialogue:

Dim response As String = Nothing

Using dlg As New Dialog
    If dlg.ShowDialog() = DialogResult.OK Then
        response = dlg.Response
    End If
End Using

'...

Now you don't need a Click event handler for your Button as you can just set its DialogResult property to OK in the designer.

Upvotes: 2

Related Questions