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

visual studio-(vb.net)当我单击另一种形式的按钮时如何退出子?

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

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

我正在逐步学习vb.net。我有两种形式,我想编写一个代码,即当我单击另一种形式的Button1时,然后是我的主子出口。我已经在Google中搜索过,但没有找到任何东西。我该如何解决这条线?

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

使用的示例DialogResult

在Form1中:

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

在Form2中:

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

请注意,如果用户取消Form2对话框而不点击按钮,则将得到Cancel返回结果