我有一个Windows表单应用程序,该应用程序将打开其他表单,但只会显示几秒钟的表单(用户可配置)。我通常会做类似螺纹的事情。直到线醒来,输入将不会采取行动。

我遇到了使用system.timers.timer(n)的人,但是我正在努力使它为我工作,该表格只会立即打开和关闭(您只能在打开表单然后关闭时看到闪光灯) 。

我使用的代码是:

Private Shared tmr As New System.Timers.Timer    
aForm.Show()
tmr = New System.Timers.Timer(aSleep * 60 * 60)
tmr.Enabled = True

aForm.Close()

所有这些都包含在传递表单和定义运行时间的私人子中。

我的目的是让任务栏运行的主应用程序,然后调用将显示在定义的时间段内显示的表单之一,关闭表单,然后调用另一个表单。

是否有能力将我指向正确的方向,以便为什么该表格打开然后关闭而无需看到定义的运行时间(我一直在使用10秒钟进行测试),还是有更好的方法来做我正在寻找的事情?

非常感谢您的帮助。

马特

有帮助吗?

解决方案

文档说,有一个经过的事件处理程序,当时间流逝时被调用。您将关闭处理程序中的表格:

http://msdn.microsoft.com/en-us/library/system.timers.timer%28vs.85%29.aspx

我只是写了一个小示例,该示例显示了您需要做的事情:

http://www.antiyes.com/close-form-after-10秒

以下是相关代码,可以从文章中下载完整的解决方案。

表1代码

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm2 As New Form2()
        frm2.ShowDialog()
    End Sub

End Class

表2代码

Imports System.Timers

Public Class Form2

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)
        Dim tmr As New System.Timers.Timer()
        tmr.Interval = 5000
        tmr.Enabled = True
        tmr.Start()
        AddHandler tmr.Elapsed, AddressOf OnTimedEvent
    End Sub

    Private Delegate Sub CloseFormCallback()

    Private Sub CloseForm()
        If InvokeRequired Then
            Dim d As New CloseFormCallback(AddressOf CloseForm)
            Invoke(d, Nothing)
        Else
            Close()
        End If
    End Sub

    Private Sub OnTimedEvent(ByVal sender As Object, ByVal e As ElapsedEventArgs)
        CloseForm()
    End Sub

End Class

当然,要使用此代码,您需要使用按钮设置表单。

其他提示

您的代码设置计时器,然后立即关闭表单。计时器事件启动时必须完成结束。

我想我可以对乔纳森的答案进行扩展。

在您希望以给定时间显示的表单上,添加一个计时器(在此示例中,计时器命名为Timer1 ...可以在工具包中找到计时器,只需将其拖动到表单上)

在给定的时间内显示表单后,以表格的on load方法启动计时器:

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

我希望这会有所帮助,让我知道我是否可以以更清晰的方式重述任何内容:-)

只需将时间组件贴在表单上并启用即可。在tick事件处理程序中,确定表格打开以来的时间以及预期的期间是否具有弹性,请关闭表格。

通过在等待tick事件时允许Form_load手柄返回,允许表单绘画并执行通常会绘制的其他所有操作。

虽然您可以按照您的代码创建时间,但我不确定为什么会。而且,您绝对需要为tick活动设置一个处理程序,以便它做任何好事。

如果frmtemperaturestatus关闭,则打开设定时间(在此示例中)打开表单的真正简单方法将跳过计时器。我正在将Frmtemperatulestatus作为正常形式打开,而不是作为对话框,否则代码会跳至此表单,并且直到关闭表单之前才返回。 doevents保持frmtemperatulestatus响应能力(注意:Frmtemperaturestatus如果您按行测试代码,因为焦点不断返回Visual Studio,则将继续失去焦点。

            timeIncubation_End_Time = Now.AddMinutes(1)
            Me.Enabled = False
            frmTemperature_Status.Show()

            Do While frmTemperature_Status.Visible And timeIncubation_End_Time > Now
                Application.DoEvents()
                Threading.Thread.Sleep(100)
            Loop
            frmTemperature_Status.Close() ' This line doesn't cause an error if the form is already closed
            Me.Enabled = True
            MsgBox("End of dialog test")
Public Class Form1
    Dim second As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 1000
        Timer1.Start() 'Timer starts functioning
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label1.Text = DateTime.Now.ToString

        second = second + 1
        If second >= 10 Then
            Timer1.Stop() 'Timer stops functioning
            Me.Close()
            MsgBox("Timer Stopped....")
        End If

    End Sub

End Class
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top