如何从VB.NET中的对话框表单中获取值?

Max*_*Max 11 vb.net textbox dialog modal-dialog winforms

我有一个"frmOptions"表单,其中包含一个名为"txtMyTextValue"的文本框和一个名为"btnSave"的按钮,用于在单击时保存并关闭表单,

然后,当在主窗体"frmMain"上单击一个按钮"btnOptions"时,我正在显示此对话框"frmOptions",就像这样

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
Run Code Online (Sandbox Code Playgroud)

当单击"btnSave"时,如何在主窗体"frmMain"中插入文本框"txtMyTextValue"中插入的值?

Kar*_*son 25

只有在结果是OK(用户按下Save而不是以Cancel其他方式关闭对话框)时才想从对话框中捕获信息,所以这样做:

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
Run Code Online (Sandbox Code Playgroud)

现在,在对话框窗体中,Windows.Forms.DialogResult.OK当用户单击OK与对话框窗体的操作对应的按钮时,您需要设置结果,如下所示:

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
Run Code Online (Sandbox Code Playgroud)