我想我可以稍微扩展一下乔纳森的回答。
在您希望在给定时间内显示的表单上,添加一个计时器(在此示例中,计时器名为 Timer1...计时器可以在工具箱中找到,只需将其拖到表单上)
要让表单在显示给定时间后关闭,在表单的 onload 方法中启动计时器:
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Do initialization stuff for your form...
'Start your timer last.
Timer1.Start()
End Sub
这将启动您的计时器。当预设时间过去时,tick 事件将触发。在这种情况下,放置您的表单关闭代码:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'Close the form after 1 tick.
Me.Close()
End Sub
要更改在计时器滴答之前应该经过多少时间,请更改计时器间隔属性。
'Changing the time from outside the Form1 class...
Form2.Timer1.Interval = 2000 '2 seconds, the interval is in milliseconds.
完整代码,form1有一个按钮,设置定时器间隔,然后打开form2。
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form2.Timer1.Interval = 2000
Form2.Show()
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Do initialization stuff for your form...
'Start your timer last.
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
End Class
我希望这会有所帮助,如果我能以更清晰的方式重述任何内容,请告诉我 :-)