msgbox that disappears automatically after certain time

Viewed 52057

Is there any type of msgbox in 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?

9 Answers

Linendra Soni's answer is good, but it may or may not work in the newer versions of Windows and/or 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

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.

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

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.

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.

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.

    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
Related