Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 555
LabRat Avatar asked Nov 29 '22 12:11

LabRat


2 Answers

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
like image 135
Idle_Mind Avatar answered Dec 06 '22 02:12

Idle_Mind


Use ShowDialog it Shows the form as a modal dialog box.

You can check the output result from the form using DialogResult

Public Sub ShowMyDialogBox()
    Dim testDialog As New Form2()

    ' Show testDialog as a modal dialog and determine if DialogResult = OK.
    If testDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
        ' Read the contents of testDialog's TextBox.
        txtResult.Text = testDialog.TextBox1.Text
    Else
        txtResult.Text = "Cancelled"
    End If
    testDialog.Dispose()
End Sub 'ShowMyDialogBox

To assign the DialogResult to a button just use the button property: button.DialogResult

like image 23
Carlos Landeras Avatar answered Dec 06 '22 02:12

Carlos Landeras