Vb.net 显示Form2在Form1关闭后延迟7秒
我的应用程序中有两个表单,单击表单1中的按钮即可显示表单2。但是我需要在form1的关闭和form2的展示之间延迟7秒,为此我编写了以下代码:Vb.net 显示Form2在Form1关闭后延迟7秒,vb.net,winforms,Vb.net,Winforms,我的应用程序中有两个表单,单击表单1中的按钮即可显示表单2。但是我需要在form1的关闭和form2的展示之间延迟7秒,为此我编写了以下代码: 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.
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()
我认为不需要对时间延迟进行不必要的声明,因为您只需在
计时器控件上设置它
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
或
在完成运行之前,您正在关闭包含计时器的表单1。尝试将计时器和代码移动到表单2,将其隐藏7秒。
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