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.

Dani AI

Generated

For : the most robust solution is to separate the math/work from the forms and chain the two processing steps in code instead of simulating UI clicks or relying only on filesystem notifications. 's suggestion to start the second form is fine for a quick fix, and 's FileSystemWatcher can work for decoupled processes, while 's idea of running the work on form load is simple—each approach has tradeoffs (race conditions, multiple watcher events, or blocking the UI). A clearer, safer pattern is: put the processing in a worker class, raise a completion event when writing is finished, and have the UI react (create Form2 and hand it the file path or data).

Example skeleton (keep the heavy math out of the form):

Public Class ImageProcessor
    Public Event Finished(outputPath As String)

    Public Sub DoFirstStep(inA As String, inB As String, outPath As String)
        ' perform calculation and write result safely:
        '  - write to a temp file
        '  - then atomically move/rename to outPath
        ' Raise Finished when done
        System.IO.File.WriteAllText(outPath, "PLACEHOLDER") ' replace with actual write
        RaiseEvent Finished(outPath)
    End Sub
End Class

A common real-world problem is the second form opening while the file is still being written. Use a quick readiness test before reading:

Private Function IsFileReady(path As String) As Boolean
    Try
        Using fs = System.IO.File.Open(path, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
            Return True
        End Using
    Catch ex As IO.IOException
        Return False
    End Try
End Function

Practical tips: run processing off the UI thread (Task.Run or BackgroundWorker), subscribe to the processor's Finished event, and in that handler switch to the UI thread (Invoke) to instantiate Form2 and call a public method like StartProcessing(filePath). Avoid simulating button clicks or repeatedly polling without checks. If you use FileSystemWatcher, add a short delay and verify the file is ready (use the readiness test or temp->move pattern). Finally, disable controls while processing, log exceptions, and prefer passing data in-memory when possible to avoid I/O races.

Recommended Answers

All 7 Replies

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 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, which reads file3.img (outputted by ) 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 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 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 it will do its job but when it is done, it will execute 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 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, which reads file3.img (outputted by ) 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 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 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 it will do its job but when it is done, it will execute 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.