【问题标题】:(vb.net) how to exit sub when I click button which is in another form?(vb.net)当我单击另一种形式的按钮时如何退出子?
【发布时间】:2020-11-28 13:51:48
【问题描述】:

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

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

【问题讨论】:

    标签: vb.net visual-studio


    【解决方案1】:

    一个使用DialogResult的例子。

    在表格 1 中:

    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 的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-05
      • 1970-01-01
      • 2014-06-28
      • 2011-09-05
      • 2016-01-30
      • 2019-05-07
      • 2016-11-29
      • 2022-12-31
      相关资源
      最近更新 更多