在VBA中更新用户窗体中的时间时删除闪烁的光标

Removing the blinking cursor when updating time in user form in VBA

提问人:Sofy Mastertech 提问时间:10/23/2023 最后编辑:Mayukh BhattacharyaSofy Mastertech 更新时间:10/23/2023 访问量:51

问:

我使用用户表单创建了一个日历,并在其中放置了一个使用标签的时间跟踪器,称为使用函数时是否有某种方法可以消除光标闪烁lblCurrentTimeApplication.OnTime

以下代码可能是光标闪烁的原因,我想消除这种影响。

Option Explicit
    'Set global variable
    Dim UpdateTime As Double
    Dim LastUpdateTime As Double

Sub StartTimer()
    ' Set the initial time and schedule the first update
    UpdateTime = Now + TimeValue("00:00:01")
    LastUpdateTime = Now
    Application.OnTime UpdateTime, "UpdateCurrentTime"
End Sub

Sub UpdateCurrentTime()
    ' Update the label with the current time
    frmCalendar.lblCurrentTime.Caption = Format(Now(), "hh:mm AM/PM")

    ' Calculate the time until the next update
    LastUpdateTime = LastUpdateTime + TimeValue("00:00:01")
    UpdateTime = LastUpdateTime + TimeValue("00:00:01")

    ' Schedule the next update
    Application.OnTime UpdateTime, "UpdateCurrentTime"
End Sub

Sub StopTimer()
    ' Clear any scheduled updates
    On Error Resume Next
    Application.OnTime UpdateTime, "UpdateCurrentTime", , False
    On Error GoTo 0
End Sub
Excel VBA 表单 游标

评论

0赞 vbakim 10/23/2023
您的 Application.OnTime 似乎没有问题,更新标签后尝试 Doevents。
0赞 Siddharth Rout 10/23/2023
光标闪烁?哪里?
0赞 Sofy Mastertech 10/23/2023
加载用户表单(就像日历)并尝试在工作表中添加日期时,我看到我的光标闪烁“更新时间子”我无法解决问题,也许是应用程序的内置行为。
0赞 JohnM 10/23/2023
但是光标闪烁时在哪里......在工作表的单元格中,在用户窗体的控件中?请具体说明。另外,您能否澄清一下“看到我的光标”是什么意思,您实际上是指插入符号(即您正在打字的垂直闪烁线),还是指鼠标指针,或者您的意思是您实际上看到的是文本“更新时间子”?请注意,如果有助于澄清这些要点,则可以将 GIF 添加到您的问题中。
0赞 Sofy Mastertech 10/24/2023
我衷心感谢您协助我处理此事。它非常有价值。为了进一步澄清我的问题,当我提到“查看我的光标”时,我确实指的是在加载包含标签的用户窗体时光标在工作表中闪烁,该标签用于使用 application.ontime + timevalue(“00:00:01”) 显示和更新时间:

答: 暂无答案