Frage

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)

War es hilfreich?

Lösung

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

Andere Tipps

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

public cerrar

Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

form2.show()

Do
cerrar = form2.Visible
Loop Until cerrar = False

continue codigo

End Sub

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top