I have created 2 forms. The first one is the button that you want to back up. In the second there are paths that can be modified. How to make a reference that after pressing the "backup" button will get a path of 2 forms. The path is saved when I closed form2 I know how to do it in one form but unfortunately I can not refer to another form.
Source of Form 2:
Private Sub Browser_from1_Click(sender As Object, e As EventArgs) Handles Browser_from1.Click
Dim FolderBrowserDialog1 As New FolderBrowserDialog
FolderBrowserDialog1.ShowDialog()
TextBox1from.Text = FolderBrowserDialog1.SelectedPath
If Browser_from1.Text <> "" And TextBox1from.Text <> "" Then
Backup.StartCopy.Enabled = True
End If
End Sub
Private Sub Browser_to1_Click(sender As Object, e As EventArgs) Handles Browser_to1.Click
Dim FolderBrowserDialog1 As New FolderBrowserDialog
FolderBrowserDialog1.ShowDialog()
TextBox2to.Text = FolderBrowserDialog1.SelectedPath
If Browser_to1.Text <> "" And TextBox2to.Text <> "" Then
Backup.StartCopy.Enabled = True
End If
End Sub
Private Sub TextBox1from_TextChanged(sender As Object, e As EventArgs) Handles TextBox1from.TextChanged
End Sub
Private Sub save_settings_Click(sender As Object, e As EventArgs) Handles save_settings.Click
My.Settings.pathmem = TextBox2to.Text
My.Settings.pathmem1 = TextBox1from.Text
My.Settings.Save()
End Sub
Private Sub setting_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1from.Text = My.Settings.pathmem1
TextBox2to.Text = My.Settings.pathmem
End Sub
End Class
You dont want to create a reference to a form - that would (or could) create a whole new form. You want to hold onto the form reference.
This is done by passing a reference to the forms, but the talk of one form fiddling with the controls on another form is a bad idea because it breaks encapsulation. But forms are classes (it says so at the top of each one), so you can add Properties and Methods (
Sub
and/orFunction
s) to facilitate passing information back and forth.Method One - Passing a Form Reference
The simplest way is to pass whatever the other form needs in the constructor:
In order for this to work, you need to modify the constructor (
Sub New
) on the destination form:It is best not to create your own constructor until you are familiar with them. Use the drop downs at the top of the code window: from the left pick the form name; from the right, select New. The designer adds required code to them which must not be changed.
Do not add any code before the
InitializeComponent()
call at least until you are familiar with the life cycle of a form. The form and its controls do not exist until that runs.To return to the "main" form:
You may want to remove some of the title bar buttons or add code to the form
Closing
event to handle when the user closes via the system menu or buttons.Using the constructor is ideal for cases where there is some bit of data which the form must have in order to do its job.
Method Two - Passing Data
Thats all well and good, but what about passing data to another form? You can use the constructor for that too. In order to pass say, a string, integer and a
Point
:Call it like this:
Result:
Method Three: Properties
Thats fine, but if there is a lots of data that way can get cumbersome. Plus, you may want to update some of the data from the calling/main form. For this you can create
Properties
on the form to handle the data:Rather than a private variable to act as the backing field, one of the controls is used. The name leaves a bit to be desired as it exposes implementation details. So, use names which describe what the data represents rather than where it displays.
A
point
was used just to show that other data types can be used. Setting those values from the calling/original/source form:The destination controls would well have been TextBoxes for the user to edit. The
Property
"wrappers" allow you to fetch those values back, so in this case, aDialog
was used.Method Four: Methods
You can also use methods as a way to pass data to the second/helper form. Here a
List(of T)
collection will be passed. In the child/display form a method is added to receive the data which it then displays. The task represented is proofing or viewing a filtered list:In the main/calling form:
elsewhere...perhaps in a Click event:
Result:
With DataBased apps a modified version of this can allow for the case where you display DataGridView data in detail form on another form. You need not have the second form rung SQL to add or update the record, and then the main form running another query to "refresh" the display. If the
DataSource
is aDataTable
backed up by a fully configuredDataAdapter
, pass the DataTable and have the child form add, change or delete using that. The data will automagically be in theDataTable and
DataGridView`.There are other ways to do this, but they generally all boil down to passing something from A to B. Which way is "best" depends on what the app does, the use-case and the nature of the data. There is no one right way or best way.
For instance,
Properties
and in many casesFunctions
allow the B Form to close the feedback loop. With DB items, aDataChanged
property might tell the calling form that data was added or changed so that form knows to use theDataAdapter
to update the db.