Support Forums

Full Version: [Snippet] Autoclose Msgbox
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have derived this code from this post.
Code:
http://www.vbforums.com/showpost.php?p=3745046&postcount=5

And created a simple function for autoclosing msgbox.
Usage:
Code:
msgboxautoclose("Sample message goes here", MsgBoxStyle.Information, "Title Message", 20)

20 is the delay to close the msgbox

Code:
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte,
                       ByVal bScan As Byte, _
                       ByVal dwFlags As Byte, _
                       ByVal dwExtraInfo As Byte)

    Private Const VK_RETURN As Byte = &HD
    Private Const KEYEVENTF_KEYDOWN As Byte = &H0
    Private Const KEYEVENTF_KEYUP As Byte = &H2

    Public Sub msgboxautoclose(ByVal Message As String, Optional ByVal Style As MsgBoxStyle = Nothing, Optional ByVal title As String = Nothing, Optional ByVal delay As Integer = 5)
        Dim t As New Threading.Thread(AddressOf closeMsgbox)
        t.Start(delay) '5 second default delay
        MsgBox(Message, Style, title)
    End Sub

    Private Sub closeMsgbox(ByVal delay As Object)
        Threading.Thread.Sleep(CInt(delay) * 1000)
        AppActivate(Me.Text)
        keybd_event(VK_RETURN, 0, KEYEVENTF_KEYDOWN, 0)
        keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0)
    End Sub

Credits goes to paul and modification from me.

Thanks for sharing. Not sure why would use this but it seems to work as stated. To make this simpler, I'm sure you could use SendKeys().
I am sure I can find this useful somehow thanks for this.
(05-18-2011, 01:02 PM)KoBE Wrote: [ -> ]Thanks for sharing. Not sure why would use this but it seems to work as stated. To make this simpler, I'm sure you could use SendKeys().

Sendkeys is not working if the msgbox is not on focus.
This wouldn't either without
Code:
AppActivate(Me.Text)

so put that in and SendKeys should work.
I think this had been posted before, I saw this when I clicked the Search Button Smile
(05-20-2011, 06:42 AM)stephen5565 Wrote: [ -> ]I think this had been posted before, I saw this when I clicked the Search Button Smile

I guess we are of different approach.
Thanks, I was looking for this.~
Was too lazy to search Google :]