Private Sub btnOK_Click(sender As Object, e As RoutedEventArgs)
AutoClosingMessageBox.Show("Wrong Input.", "LMS", 5000)
End Sub
Public Class AutoClosingMessageBox
Private _timeoutTimer As System.Threading.Timer
Private _caption As String
Private Sub New(text As String, caption As String, timeout As Integer)
_caption = caption
_timeoutTimer = New System.Threading.Timer(AddressOf OnTimerElapsed, Nothing, timeout, System.Threading.Timeout.Infinite)
MessageBox.Show(text, caption)
End Sub
Public Shared Sub Show(text As String, caption As String, timeout As Integer)
New AutoClosingMessageBox(text, caption, timeout)
End Sub
Private Sub OnTimerElapsed(state As Object)
Dim mbWnd As IntPtr = FindWindow(Nothing, _caption)
If mbWnd <> IntPtr.Zero Then
SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero)
End If
_timeoutTimer.Dispose()
End Sub
Const WM_CLOSE As Integer = &H10
<System.Runtime.InteropServices.DllImport("user32.dll", SetLastError := True)> _
Private Shared Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr
End Function
<System.Runtime.InteropServices.DllImport("user32.dll", CharSet := System.Runtime.InteropServices.CharSet.Auto)> _
Private Shared Function SendMessage(hWnd As IntPtr, Msg As UInt32, wParam As IntPtr, lParam As IntPtr) As IntPtr
End Function
End Class