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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20551203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:19:55  来源:igfitidea点击:

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

vb.netwinformstextboxdialogmodal-dialog

提问by Max

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,

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

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

然后,当在主窗体“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

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

当单击“btnSave”时,如何在主表单“frmMain”中获取插入到文本框“txtMyTextValue”中的值?

回答by Karl Anderson

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

你想捕捉从该对话框中的信息仅当结果是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

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

现在在您的对话框表单中,您需要设置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

回答by N0Alias

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:

在 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:

在 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

回答by Steve

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

最简单的方法是在 frmOptions 表单中添加一个公共属性,该属性返回在 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

然后,当您的用户单击 OK 按钮确认其选择时,您将文本框的值复制到内部变量

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

最后在 frmMain 中使用这样的代码来检索插入的值

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.

我更喜欢避免直接访问 frmOptions 的内部控件,该属性提供了一个间接性,可用于更好地验证用户提供的输入。

回答by user3096633

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

您可以从 frmOptions 实例访问该值。然而,这违反了得墨忒耳定律。

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

您应该使用类中的属性公开该值。公共类 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.

最后,如果您使用的是对话框,请确保为按钮设置对话框结果。