Hi guys,
I would appreciate it if you could give me sample code for linking two forms. I have a program which has 2 forms. I execute form1 first because it reads files and then writes to a file. Then I execute second form (form2) that will read the file created by form1.

I would appreciate it if someone provides a sample code regarding form1 executes form2 once it finishes writing to a file.

How could I make form1 start form2 once it finishes reading file and writing to a file?

Thanks.

Well, if you have the code for read/write, you can put at the end "Form2.Show" and "Me.Close".

I think that this is the best way to do it.

You could also add a FileSystemWatcher to your project. In the coding you just add the following properties:

' Set the FileSystemWatcher to monitor a specific directory 
FileSystemWatcher1.Path = CurDir() & "\Data\"
' Watch only for changes to a certain file (eg. Setup.accdb file)
FileSystemWatcher1.Filter = "Setup.accdb"
' Enable the component to begin watching for changes.
FileSystemWatcher1.EnableRaisingEvents = True

Then In your

Private Sub FileSystemWatcher1_Changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
        ' Open the next form here, or do what ever coding you want here
End Sub

Then just dispose of it when you are done using:

FileSystemWatcher1.Dispose()

Thanks Mike for your reply. I do appreciate for your help but I think I cannot explain my problem well.

I have a form1.vb which reads 2 files (file1.img and file2.img) then applies a math function and saves the results to a file (file3.img). I have another form, form2.vb which reads file3.img (outputted by form1.vb) and apply another math function then saves the result to a file (file4).

I do these calculations in two separate forms but not automatically. I mean I execute form1.vb and select two files to be read in, then select the path to save the output then click OK button and execute and I get the output. Then I open form2.vb and select the file outputted by previous form, and select the path to save the result and then click OK button and get the final output.

My question is I want to code so that when I run form1.vb it will do its job but when it is done, it will execute form2.vb and do the calculations and get the final output. We can say that I want to execute once and all...

Thanks though.

Thanks Mike for your reply. I do appreciate for your help but I think I cannot explain my problem well.

I have a form1.vb which reads 2 files (file1.img and file2.img) then applies a math function and saves the results to a file (file3.img). I have another form, form2.vb which reads file3.img (outputted by form1.vb) and apply another math function then saves the result to a file (file4).

I do these calculations in two separate forms but not automatically. I mean I execute form1.vb and select two files to be read in, then select the path to save the output then click OK button and execute and I get the output. Then I open form2.vb and select the file outputted by previous form, and select the path to save the result and then click OK button and get the final output.

My question is I want to code so that when I run form1.vb it will do its job but when it is done, it will execute form2.vb and do the calculations and get the final output. We can say that I want to execute once and all...

Thanks though.

When you click the OK button on Form1, after you have selected the 2 files, this performs your calculation. I would then run the FileSystemWatcher that I mentioned earlier from Form1, but set it to monitor the directory where your file (file3.img) is saved to. Also create a variable to store this directory location (eg. imgLoc). Therefore, in the FileSystemWatcher1.Changed event (it will pick up any changes to this directory specified), you would then add code to open Form2, and run the necessary code from there:

My.Forms.Form2.Visible = True
My.Forms.Form2.ImgFile3Location.Text = imgLoc  'Set the file location to img3
My.Forms.Form2.OKButton.PerformClick()  'Perform the execution
Me.Close()  'Close the first form

And then add or modify to your preferences.

Alternatively to what Mike has said, if you move your executing code (in form2) to the form "on load" section (of form2) instead of in a separate sub it will execute as form2 starts up.

Is that what your after cs_tx_usa?

Its a simpler way of doing what Mike has said.

Edited 5 Years Ago by Mike Askew: Clarifying which form the code is being moved in

This article has been dead for over six months. Start a new discussion instead.