vb.net 10 秒后关闭表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4506033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:12:37  来源:igfitidea点击:

Close form after 10 seconds

vb.net.net-3.0

提问by Lima

I have a windows form application that will open other forms, but will only display the forms for a few seconds (user configurable). I would normally do something like threading.thread.sleep(n), however when doing this the forms controls do not load only the white background shows, and I have also been reading that this isnt the best practice for what I am after as user input will not be actioned until the thread wakes up.

我有一个 windows 窗体应用程序,它将打开其他窗体,但只会显示窗体几秒钟(用户可配置)。我通常会做一些类似 threading.thread.sleep(n) 的事情,但是当这样做时,表单控件不会只加载白色背景显示,而且我也一直在读到这不是我作为用户所追求的最佳实践在线程唤醒之前,不会对输入进行操作。

I have come across people using System.Timers.Timer(n), but I am struggling to get this to work for me, the form will only open and close straight away (you can only see a flash as the form opens then closes).

我遇到过使用 System.Timers.Timer(n) 的人,但我正在努力让它对我来说有效,表单只会立即打开和关闭(当表单打开然后关闭时,您只能看到闪光) .

The code that I am using is:

我正在使用的代码是:

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

aForm.Close()

This is all contained within a Private sub that passes the form and the defined run time.

这一切都包含在传递表单和定义的运行时间的 Private 子程序中。

My intention is to have the main application running from the task bar, which then calls one of the forms that will display for a defined period of time, close the form, then call another one of the forms.

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

Is any able to point me in the right direction for why the form opens then closes without seeing through the defined run time (I have been testing with 10 seconds), or is there a better way of doing what I am seeking?

有没有人能够指出为什么表单打开然后关闭而没有看到定义的运行时间(我已经测试了 10 秒)的正确方向,或者是否有更好的方法来做我正在寻找的东西?

Your help is greatly appreciated.

非常感谢您的帮助。

Matt

马特

回答by John Boker

the docs say there's an Elapsed event handler that gets called when the time elapses. You would close the form in the handler:

文档说有一个 Elapsed 事件处理程序,当时间过去时会被调用。您将在处理程序中关闭表单:

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

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

I just wrote a little example that shows what you would need to do at:

我刚刚写了一个小例子,展示了你需要做什么:

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

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

Below is the relevant code, the full solution can be downloaded from the article.

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

Form 1 code

表格 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

Form 2 code

表格 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

Of course for this code to work you'd need forms setup with the buttons.

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

回答by martona

Your code sets a timer then immediately closes the form. The closing must be done when the timer event fires.

您的代码设置一个计时器,然后立即关闭表单。关闭必须在计时器事件触发时完成。

回答by Arbiter of buffoonery

I think I can expand on Jonathan's answer a bit.

我想我可以稍微扩展一下乔纳森的回答。

On the form that you wish to display for a given amount of time, add a timer (in this example the timer is named Timer1...Timers can be found in the toolbax, just drag it onto the form)

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

To have the form close after it has been displayed for a given amount of time start the timer in the onload method of the form:

要在表单显示给定的时间后关闭表单,请在表单的 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

This will start your timer. When the preset time elapses, the tick event will fire. In this event place your form closing code:

这将启动您的计时器。当预设时间过去后,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

To change how much time should elapse before the timer ticks, change the timer interval property.

要更改计时器滴答之前应经过的时间,请更改计时器间隔属性。

'Changing the time from outside the Form1 class...
Form2.Timer1.Interval = 2000 '2 seconds, the interval is in milliseconds.

Full code, form1 has a button that sets the timer interval and then opens form2.

完整代码,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

I hope this helps, let me know if I can restate anything in a more clear fashion :-)

我希望这会有所帮助,如果我能以更清晰的方式重述任何内容,请告诉我:-)

回答by Jonathan Wood

Just stick a time component on the form and enable it. In the tick event handler, determine how long since the form opened and if the intended period has elapses, close the form.

只需在表单上粘贴一个时间组件并启用它。在滴答事件处理程序中,确定自表单打开以来的时间,以及如果预期的时间段已过,请关闭表单。

By allowing the Form_Load handle to return while you wait for the Tick event, the form is allowed to paint and do everything else it normally would.

通过允许 Form_Load 句柄在您等待 Tick 事件时返回,表单就可以进行绘制并执行通常会执行的所有其他操作。

While you could create the time from code as you are doing, I'm not sure why you would. And you definitely need to set a handler for the Tick event for it to do any good.

虽然您可以根据代码创建时间,但我不确定您为什么会这样做。并且您肯定需要为 Tick 事件设置一个处理程序才能发挥作用。

回答by Kristian

A real simple way of opening a form for a set time (and in this example) will skip timer if frmTemperatureStatus is closed. I'm opening frmTemperatureStatus as a normal form not as a dialog otherwise the code jumps to this form and doesn't return until the form is closed. The DoEvents keeps frmTemperatureStatus responsive (Note: frmTemperatureStatus will keep losing focus if you test code line by line as focus keeps going back to Visual Studio).

如果 frmTemperatureStatus 关闭,则在设定的时间(在本示例中)打开表单的一种真正简单的方法将跳过计时器。我将 frmTemperatureStatus 作为普通表单而不是对话框打开,否则代码会跳转到此表单并且在表单关闭之前不会返回。DoEvents 使 frmTemperatureStatus 保持响应(注意:如果您逐行测试代码,则 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")

回答by erik klenovics

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