vb.net 一段时间后自动消失的msgbox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6509844/
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:35:14  来源:igfitidea点击:

msgbox that disappears automatically after certain time

vb.netmsgbox

提问by Furqan Sehgal

Is there any type of msgboxin vb.net that gives a message and it disappears automatically after a certain time? Or is there any method to hide the msgbox, without user's clicking OK?

msgboxvb.net 中是否有任何类型的消息会在一段时间后自动消失?或者有什么方法可以隐藏msgbox, 而无需用户单击“确定”?

回答by Linendra Soni

You Can use

您可以使用

CreateObject("WScript.Shell").Popup("Welcome", 1, "Title")

CreateObject("WScript.Shell").Popup("欢迎", 1, "Title")

this msgbox will close automatically after 1 second

此 msgbox 将在 1 秒后自动关闭

回答by AJ.

No, I don't think there's a built-in framework control that will do this for you. However, you could easily do this with a custom-built form that fires a timer in it's Loadevent. Then, when the set amount of time has passed, in the timer Elapsedevent, you can simply close the form.

不,我认为没有内置的框架控件可以为您执行此操作。但是,您可以使用定制的表单轻松完成此操作,该表单在其Load事件中触发计时器。然后,当设定的时间过去后,在计时器Elapsed事件中,您可以简单地关闭表单。

回答by Ibo

Linendra Soni's answer is good, but it may or may not work in the newer versions of Windows and/or Excel.

Linendra Soni 的回答很好,但它在较新版本的 Windows 和/或 Excel 中可能有效,也可能无效。

This works perfectly in the newer versions:

这在较新版本中完美运行:

Function MessageTimeOut(sMessage As String, sTitle As String, iSeconds As Integer) As Boolean
    Dim Shell
    Set Shell = CreateObject("WScript.Shell")
    Shell.Run "mshta.exe vbscript:close(CreateObject(""WScript.shell"").Popup(""" & sMessage & """," & iSeconds & ",""" & sTitle & """))"
    MessageTimeOut = True
End Function

Use it like this:

像这样使用它:

Sub Example()
    Dim chk As Boolean
    chk = MessageTimeOut("Hello!", "Example Sub", 1) 'if chk returned FALSE that means the function was not executed successfully
End Sub

or

或者

Sub Example()
    Call MessageTimeOut("Hello!", "Example Sub", 1) 'you don't need to get the output of the function
End Sub

Output:

输出:

enter image description here

在此处输入图片说明

回答by Ed W

Use a timer or some type of delay/sleep and after time expires run

使用计时器或某种类型的延迟/睡眠,并在时间到期后运行

SendKeys.Send("~")

This is the same has hitting the ENTER key.

这与按 ENTER 键相同。

You may need to make it proceed it by activating the msgbox window again.

您可能需要通过再次激活 msgbox 窗口来使其继续。

回答by Alexis Martial

Inspired by the answers, this is what I came with, working nicely in simple cases, allowing to use all MsgBox features directly:

受到答案的启发,这就是我带来的,在简单的情况下很好地工作,允许直接使用所有 MsgBox 功能:

Imports System.Threading

Module FormUtils
    Private sAutoClosed As Boolean

    Private Sub CloseMsgBoxDelay(ByVal data As Object)
        System.Threading.Thread.Sleep(CInt(data))
        SendKeys.SendWait("~")
        sAutoClosed = True
    End Sub

    Public Function MsgBoxDelayClose(prompt As Object, ByVal delay As Integer, Optional delayedResult As MsgBoxResult = MsgBoxResult.Ok, Optional buttons As MsgBoxStyle = MsgBoxStyle.ApplicationModal, Optional title As Object = Nothing) As MsgBoxResult
        Dim t As Thread

        If delay > 0 Then
            sAutoClosed = False
            t = New Thread(AddressOf CloseMsgBoxDelay)
            t.Start(delay)

            MsgBoxDelayClose = MsgBox(prompt, buttons, title)
            If sAutoClosed Then
                MsgBoxDelayClose = delayedResult
            Else
                t.Abort()
            End If
        Else
            MsgBoxDelayClose = MsgBox(prompt, buttons, title)
        End If

    End Function
End Module

PS: You must add this to yourApp.config file:

PS:您必须将其添加到 yourApp.config 文件中:

<appSettings> <add key="SendKeys" value="SendInput"/> </appSettings>

<appSettings> <add key="SendKeys" value="SendInput"/> </appSettings>

回答by MitchP12

You can do this by adding a Timer to your form. 'Timer to autoclose after 100 ms Dim seconds As Integer = 100

您可以通过在表单中​​添加一个 Timer 来做到这一点。'定时器在 100 ms 后自动关闭 Dim seconds As Integer = 100

'Existing code....
 Timer1.Start()
 MessageBox.Show("Window Timed Out", "TimeOut")
 Me.Close()

'Tick Event Code
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As 
             System.EventArgs) Handles Timer1.Tick
    seconds = seconds - 1
    If seconds < 1 Then`    
       Me.Close()
    End If
End Sub

回答by Thongchai Shiesathanakun

I have some code to show file updated time and close message box within 3 sec. Please see below. I hope that this code can support this topic.

我有一些代码来显示文件更新时间并在 3 秒内关闭消息框。请参阅下文。我希望这段代码可以支持这个话题。

    Sub Workbook_Open()
    Application.ScreenUpdating = False
    SplashUserForm.Show
    Windows(ThisWorkbook.Name).Visible = True
    Application.ScreenUpdating = True

    last_update = "Last updated : " & Format(FileDateTime(ThisWorkbook.FullName), "ddd dd/mm/yy hh:mm ampm")

    'Close message after time if no action!
    Dim myTimedBox As Object
    Dim boxTime%, myExpired%, myOK%, myQuestBox%

    'Access timed message box.
    Set myTimedBox = CreateObject("WScript.Shell")
    boxTime = 3


    'User Selected "OK."
    If myQuestBox = 1 Then
    'Add any code in place of code below for this condition!
        myOK = myTimedBox.Popup(last_update & vbCr & "Do nothing and this message will close in 3 seconds.", _
        boxTime, "You Took Action!", vbOKOnly)

    Else

    'User took no Action!
        myExpired = myTimedBox.Popup(last_update & vbCr & "Do nothing and this message will close in 3 seconds.", _
        boxTime, "No Action Taken!", vbOKOnly)
    End If


End Sub

回答by Vecihi Baltac?

I dont think there is a tool such as that. But I think you can do that with follow this steps;

我不认为有这样的工具。但我认为你可以按照以下步骤做到这一点;

  1. Create an instance of Form element, and design it like a messagebox.
  2. In Form load event, get the system time or start the timer with interval value.
  3. This timer tick how many seconds you want then call the Form Close event.
  1. 创建一个 Form 元素的实例,并将其设计成一个消息框。
  2. 在表单加载事件中,获取系统时间或使用间隔值启动计时器。
  3. 此计时器勾选您想要的秒数,然后调用 Form Close 事件。

P.S : If I'm wrong, I'm sorry. I only try to solve something, maybe there is a better way to solve your problem.

PS:如果我错了,我很抱歉。我只是尝试解决一些问题,也许有更好的方法来解决您的问题。