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,
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
How can I get in the main form "frmMain" the value inserted in the textbox "txtMyTextValue" when the "btnSave" is clicked?
You can access the value from the frmOptions instance. However, this breaks the law of demeter.
You should expose the value with a property within your class. Public Class frmOptions
End Class
Then you can access the value:
Finally, if you are using a Dialog then make sure to set the Dialog Result for the button.
You want to capture the information from the dialog only if the result is
OK
(user pressesSave
instead ofCancel
or closes the dialog some other way), so do this: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 theOK
action of the dialog form, like this: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:
In frmMain:
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
Then, when your user clicks the OK button to confirm its choices you copy the value of the textbox to the internal variable
Finally in the frmMain you use code like this to retrieve the inserted value
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.