如何从 VB.NET 中的对话框中获取值? [英] How to get values from a dialog form in VB.NET?
问题描述
我有一个frmOptions"表单,其中包含一个名为txtMyTextValue"的文本框和一个名为btnSave"的按钮,用于在单击时保存和关闭表单,
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,
然后,当单击主窗体frmMain"上的按钮btnOptions"时,我将显示此对话框窗体frmOptions",如下所示
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
当单击btnSave"时,如何在主窗体frmMain"中获取插入到文本框txtMyTextValue"中的值?
How can I get in the main form "frmMain" the value inserted in the textbox "txtMyTextValue" when the "btnSave" is clicked?
推荐答案
只有当结果为 OK
(用户按下 Save
而不是 Cancel
或以其他方式关闭对话框),请执行以下操作:
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
现在在您的对话框表单中,当用户单击与 OK
操作对应的按钮时,您需要设置结果 Windows.Forms.DialogResult.OK
对话框形式,如下所示:
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
这篇关于如何从 VB.NET 中的对话框中获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!