vb. net wait for 2nd form to close before continuing

LabRat picture LabRat · Oct 16, 2013 · Viewed 17.3k times · Source

Is there a way in vb.net to pause a function\ event and wait till another form has closed to contine

example:

    Private Sub B1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B1.Click

label1.text="Hello testing.." '1st function

form2.show() '2nd function

'MAKE FORM WAIT FOR FORM 2 TO CLOSE...
'THEN CONTINUE WITH THE NEXT FUNCTION

msgbox("Some function that was waiting in the event") '3rd function

end sub

The closest I can find towards what I want is an input box but an input box is limited in what I want though it dose wait as i would like the Form2 to function.

another suggestion was to loop untill the form2 has closed, however this is hacky and unprofesional as a solution (my oppinion)

Answer

Idle_Mind picture Idle_Mind · Oct 16, 2013

Just change:

form2.Show()

To:

form2.ShowDialog()

From the documentation for ShowDialog():

You can use this method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed

Note that in the simplified example above, we are not capturing the return value of ShowDialog().

We could use the return value to determine if subsequent code should be executed or not:

If form2.ShowDialog() = DialogResult.OK Then
    ' ... do something in here ...
End If