How to get values from a dialog form in VB.NET?

2019-04-20 04:29发布

问题:

I have a "frmOptions" form with a textbox named "txtMyTextValue" and a button named "btnSave" to save and close the form when it's clicked,

then, I'm showing this dialog form "frmOptions" when a button "btnOptions" is clicked on the main form "frmMain", like this

Private Sub btnOptions_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOptions.Click
    ShowOptionsForm()
End Sub

Private Sub ShowOptionsForm()
    Dim options = New frmOptions
    options.ShowDialog()
End Sub

How can I get in the main form "frmMain" the value inserted in the textbox "txtMyTextValue" when the "btnSave" is clicked?

回答1:

You want to capture the information from the dialog only if the result is OK (user presses Save instead of Cancel or closes the dialog some other way), so do this:

Private Sub ShowOptionsForm()
    Dim options = New frmOptions

    ' Did the user click Save?
    If options.ShowDialog() = Windows.Forms.DialogResult.OK Then
        ' Yes, so grab the values you want from the dialog here
        Dim textBoxValue As String = options.txtMyTextValue.Text
    End If
End Sub

Now inside of your dialog form, you need to set the result Windows.Forms.DialogResult.OK when the user clicks the button that corresponds to the OK action of the dialog form, like this:

Public Class frmOptions
    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        ' Set the result to pass back to the form that called this dialog
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub
End Class


回答2:

You can use Events to take care of this. With this approach the Settings Form does not have to be Modal and the user can click the Save Button at any time.

In frmOptions:

'You can expand the signature to take more than just a single String.
Friend Event SavedOptions(ByVal strData As String)

Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

    RaiseEvent SavedOptions(txtMyTextValue.Text)

End Sub

In frmMain:

Private Sub ShowOptionsForm()

    Dim options = New frmOptions

    AddHandler options.SavedOptions, AddressOf OnOptionsSave

    options.ShowDialog()

End Sub

Private Sub OnOptionsSave(ByVal strData As String)

    'Or whatever you want to do on frmMain with Options Data.
    MsgBox(strData)

End Sub


回答3:

The simplest method is to add a public property to the frmOptions form that returns an internal string declared at the global level of the frmOptions

Dim strValue As String
Public Property MyStringValue() As String
    Get
       Return strValue
    End Get
End Property

Then, when your user clicks the OK button to confirm its choices you copy the value of the textbox to the internal variable

Private Sub cmdOK_Click(sender As Object, e As System.EventArgs) Handles cmdOK.Click
    strValue = txtMyTextValue.Text
End Sub

Finally in the frmMain you use code like this to retrieve the inserted value

Private Sub ShowOptionsForm()
    Using options = New frmOptions()
       if DialogResult.OK = options.ShowDialog() Then
          Dim value = options.MyStringValue
       End If
    End Using
End Sub

I prefer to avoid direct access to the internal controls of the frmOptions, a property offer a indirection that could be used to better validate the inputs given by your user.



回答4:

You can access the value from the frmOptions instance. However, this breaks the law of demeter.

You should expose the value with a property within your class. Public Class frmOptions

Public ReadOnly Property MyTextValue As String
    Get
        Return Me.txtMyTextValue.Text

    End Get
End Property

End Class

Then you can access the value:

 Private Sub ShowOptionsForm()
        Dim options = New frmOptions
        Dim frmOptionTextValue As String
        Dim frmOptionsDiagResult As DialogResult
        frmOptionsDiagResult = options.ShowDialog()
        If frmOptionsDiagResult = Windows.Forms.DialogResult.OK Then
            frmOptionTextValue = options.MyTextValue
        Else
            '...
        End If
    End Sub

Finally, if you are using a Dialog then make sure to set the Dialog Result for the button.