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?

- 2,923
- 2
- 23
- 40

- 4,917
- 33
- 108
- 167
-
Maybe you should implement your own message box and get its code to fade it off after sometime (it's easy to do that). I don't think messageboxes have that feature. – afaolek Jun 28 '11 at 16:34
-
could you plesee give me a hint how to do it? – Furqan Sehgal Jun 28 '11 at 16:40
-
I guess that answer by AJ does it. Just remember to show the form with `.showDialog()` – afaolek Jun 28 '11 at 16:51
9 Answers
You Can use
CreateObject("WScript.Shell").Popup("Welcome", 1, "Title")
this msgbox will close automatically after 1 second

- 170
- 1
- 10
-
Is there any way to have this object's methods auto-completed by Intellisense? Must it be Casted to some type? – beppe9000 Dec 27 '14 at 17:47
-
Is there a way to make it appear on top of all other windows? Or to specify a position? – cricardol Mar 08 '18 at 18:51
-
Just to add if you wanted to use this in VBA use `call` before the entire line – Ibo May 24 '19 at 18:17
-
CreateObject("WScript.Shell").Popup("YOU ARE EARLY", 2, MsgBox_String, MsgBoxStyle.Critical) we can also add style to box. – Abhay.Patil Aug 27 '19 at 06:45
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 Load
event. Then, when the set amount of time has passed, in the timer Elapsed
event, you can simply close the form.

- 16,368
- 20
- 95
- 150
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:

- 4,081
- 6
- 45
- 65
-
Ibo, I just used your code. How do I add new line? I tried vbNewLine and vbCrLf but they did not work. – XYZKLM May 12 '20 at 11:48
-
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.
-
2Could you add a little bit more to your answer, or include some sample code? – Jason Nichols Nov 15 '13 at 20:56
Inspired by the answers, this is what I came with, working nicely in simple cases, allowing to use all MsgBox features directly:
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:
<appSettings>
<add key="SendKeys" value="SendInput"/>
</appSettings>

- 67
- 1
- 4
I dont think there is a tool such as that. But I think you can do that with follow this steps;
- Create an instance of Form element, and design it like a messagebox.
- In Form load event, get the system time or start the timer with interval value.
- 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.

- 10,519
- 8
- 40
- 45

- 352
- 4
- 20
You can do this by adding a Timer to your form. 'Timer to autoclose after 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

- 1
- 1
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

- 4,970
- 8
- 20
- 35
This is the way

- 4,917
- 33
- 108
- 167
-
2We discouraged answers that composed of links only, please explain it briefly. In the case that that link has been removed your answer will just become nonsense. – Cary Bondoc Jul 06 '15 at 02:42