如何从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
(用户按保存
而不是取消
或者以其他方式关闭对话框),所以这样做是:
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
现在,在对话窗体里面,你需要设置结果 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屋!