【问题标题】:msgbox that disappears automatically after certain time特定时间后自动消失的 msgbox
【发布时间】:2011-06-28 16:29:36
【问题描述】:

vb.net 中是否有任何类型的msgbox 发出消息并在一定时间后自动消失? 或者有什么方法可以隐藏msgbox,而不需要用户点击确定?

【问题讨论】:

  • 也许您应该实现自己的消息框并让其代码在一段时间后将其淡出(这很容易做到)。我不认为消息框有这个功能。
  • 能否请您给我一个提示如何操作?
  • 我猜 AJ 的回答可以做到。请记住使用.showDialog()显示表格

标签: vb.net msgbox


【解决方案1】:

你可以使用

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

此消息框将在 1 秒后自动关闭

【讨论】:

  • 有没有办法让 Intellisense 自动完成这个对象的方法?必须转换成某种类型吗?
  • 有没有办法让它出现在所有其他窗口的顶部?还是指定位置?
  • 如果您想在 VBA 中使用它,请在整行前使用 call 来添加
  • CreateObject("WScript.Shell").Popup("YOU ARE EARLY", 2, MsgBox_String, MsgBoxStyle.Critical) 我们还可以为框添加样式。
【解决方案2】:

不,我认为没有内置的框架控件可以为您执行此操作。但是,您可以使用在其Load 事件中触发计时器的定制表单轻松完成此操作。然后,当设置的时间过去后,在计时器Elapsed 事件中,您可以简单地关闭表单。

【讨论】:

    【解决方案3】:

    Linendra Soni 的回答很好,但它在较新版本的 Windows 和/或 Excel 中可能有效,也可能无效。

    这在较新的版本中完美运行:

    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
    

    像这样使用它:

    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
    

    Sub Example()
        Call MessageTimeOut("Hello!", "Example Sub", 1) 'you don't need to get the output of the function
    End Sub
    

    输出:

    【讨论】:

    • Ibo,我刚刚使用了你的代码。如何添加新行?我尝试了 vbNewLine 和 vbCrLf 但它们不起作用。
    • str="line1" & vbneline & "line2",替换“你好!”与 str
    【解决方案4】:

    使用计时器或某种类型的延迟/睡眠,并在时间到期后运行

    SendKeys.Send("~")
    

    这与按 ENTER 键相同。

    您可能需要再次激活 msgbox 窗口以使其继续。

    【讨论】:

    • 您能否在答案中添加更多内容,或者包含一些示例代码?
    【解决方案5】:

    受答案的启发,这是我带来的,在简单的情况下工作得很好,允许直接使用所有 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:您必须将其添加到 yourApp.config 文件中:

    <appSettings> <add key="SendKeys" value="SendInput"/> </appSettings>

    【讨论】:

      【解决方案6】:

      我认为没有这样的工具。但我认为您可以按照以下步骤进行操作;

      1. 创建一个 Form 元素的实例,并将其设计成一个消息框。
      2. 在表单加载事件中,获取系统时间或以间隔值启动计时器。
      3. 此计时器记下您想要多少秒,然后调用 Form Close 事件。

      P.S : 如果我错了,我很抱歉。我只是尝试解决一些问题,也许有更好的方法来解决您的问题。

      【讨论】:

        【解决方案7】:

        您可以通过在表单中​​添加计时器来做到这一点。 '定时器在 100 毫秒后自动关闭 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
        

        【讨论】:

          【解决方案8】:

          我有一些代码可以显示文件更新时间并在 3 秒内关闭消息框。 请看下文。 希望这段代码能支持这个话题。

              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
          

          【讨论】:

            【解决方案9】:

            【讨论】:

            • 我们不鼓励仅由链接组成的答案,请简要说明。如果该链接已被删除,您的答案将变得毫无意义。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多