vb.net 中是否有任何类型的msgbox
消息会在一定时间后自动消失?或者有什么方法可以隐藏msgbox
,而无需用户单击确定?
9 回答
您可以使用
CreateObject("WScript.Shell").Popup("Welcome", 1, "Title")
此消息框将在 1 秒后自动关闭
不,我认为没有内置的框架控件可以为您执行此操作。Load
但是,您可以使用在其事件中触发计时器的定制表单轻松地做到这一点。然后,当设置的时间过去后,在计时器Elapsed
事件中,您可以简单地关闭表单。
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
输出:
使用计时器或某种类型的延迟/睡眠,并在时间到期后运行
SendKeys.Send("~")
这与按 ENTER 键相同。
您可能需要通过再次激活 msgbox 窗口来使其继续。
受到答案的启发,这就是我带来的,在简单的情况下工作得很好,允许直接使用所有 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>
我不认为有这样的工具。但我认为你可以按照以下步骤做到这一点;
- 创建 Form 元素的实例,并将其设计为消息框。
- 在表单加载事件中,获取系统时间或使用间隔值启动计时器。
- 这个计时器记下您想要多少秒,然后调用 Form Close 事件。
PS:如果我错了,我很抱歉。我只是尝试解决一些问题,也许有更好的方法来解决您的问题。
我有一些代码可以显示文件更新时间并在 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
您可以通过在表单中添加计时器来完成此操作。'定时器在 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