我有一个“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
当单击“btnSave”时,如何获取主要格式“frmMain”在文本框“txtMyTextValue”中插入的值?
您只想在对话框中捕获信息,如果结果是OK(用户按Save而不是Cancel或者以其他方式关闭对话框),这样做:
原文链接:https://www.f2er.com/vb/255125.htmlPrivate 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的结果,如下所示:
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