Inspired by the answers, this is what I came up with, working perfectly in simple cases, allowing you to directly use all the features of 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 should add this to yourApp.config file:
<appSettings> <add key="SendKeys" value="SendInput"/> </appSettings>
Alexis martial
source share