开发者

msgbox that disappears automatically after certain time

Is there any type of msgbox in vb.net that gives a message and it disappears automaticall开发者_如何学JAVAy after a certain time? Or is there any method to hide the msgbox, without user's clicking OK?


You Can use

CreateObject("WScript.Shell").Popup("Welcome", 1, "Title")

this msgbox will close automatically after 1 second


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.


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:

msgbox that disappears automatically after certain time


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.


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>


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.


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


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


This is the way

http://www.vbforums.com/showpost.php?p=3745046&postcount=5

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜