Warm tip: This article is reproduced from serverfault.com, please click

(vb.net) how to exit sub when I click button which is in another form?

发布于 2020-11-28 13:51:48

I'm learning for vb.net step by step. I have two forms and I would like to make a code that is, when I click Button1 which is in the other form, then my main sub exit. I have already searched in google, but I didn't find anything. How can I solve that line?

Public Class Form1
   Public Sub Main()
      Form2.ShowDialog()
      If Form2.Button1_Click = True then   '**This line is what I stucked**
         Exit Sub
      End if
   End Sub
End Class

Public Class Form2
   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      Me.Close()
   End Sub
End Class
Questioner
sem
Viewed
0
21.4k 2020-11-29 00:52:07

An example using DialogResult.

In Form1:

If Form2.ShowDialog = DialogResult.OK Then
    ' ... do something in here ...
End If

In Form2:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Me.DialogResult = DialogResult.OK
End Sub

Note that if the user cancels the Form2 dialog without hitting the button then you'll get a result of Cancel back instead.