在Form1关闭后延迟7秒显示Form2



我的应用程序中有两个表单,单击form1中的按钮显示form2。但我需要一个延迟7秒之间的form1的关闭和form2的显示,我写了以下代码:

   Public Class Form1
      Dim i As Integer
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      i = 0
      Me.Close()
      Timer1.Enabled = True
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
      i += 1
      If i = 7 Then
      Form1.Show()
      End If
    End Sub
  End Class

但是它没有给我结果。表格2根本没有显示。我在代码中犯了什么错误?有人能帮我吗?

当Windows窗体应用程序中没有活动表单时,该应用程序将退出。因此,您可能希望隐藏主表单,而不是关闭它:

Me.Hide()

我认为没有必要对时间延迟进行不必要的声明,因为您可以简单地在timer control上设置它

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Timer1.Interval = 7000
    Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Form2.Show()
    Me.Close()
End Sub
End Class

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Timer1.Interval = 7000
    Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Me.Close()
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    Form2.Show()
End Sub

最新更新