检测系统日期变化,视觉基础

时间:2011-11-12 11:20:44

标签: vb.net

我正在使用Visual Basics 2010在Windows-7上编写应用程序。我正在使用

访问系统日期
Dim today As Integer
today = Format(Now, "dd")

嗯,这很好用。但是当系统日期改变时我需要一些指示/通知,以便我可以检索新的日期。是否有任何功能/方法来实现这一目标? 感谢

1 个答案:

答案 0 :(得分:4)

系统日期可能会因以下两个原因而改变:

  1. 用户手动更改了系统日期/时间。可以使用此处描述的方法检测到此问题:http://vbnet.mvps.org/index.html?code/subclass/datetime.htm

  2. 时间过去了,时钟从23:59:59到00:00:00。我不知道发生这种情况时会告诉你的任何系统事件,但你可以通过在VB6中使用Timer来轻松检测到它。通过使用计时器,您将以预定义的间隔获得事件。如果日期已经改变,你可以检查一下,如果日期已经改变了 要使用标准VB6 Timer控件,您需要一个表格,您可以在其上放置您的Timer,但还有其他选择,例如:http://www.codeproject.com/KB/vb-interop/TimerLib.aspx

  3. 我的代码示例使用表单上的标准VB6 Timer来监视“分钟更改”。我的Timer控件的原始名称为Timer1

    Dim iMinute As Integer  'The "current" minute
    
    Private Sub Form_Load()
        'Initialize
        iMinute = Format(Now, "n") 'Get the current time as minute
        Timer1.Interval = 1000 'Set interval = 1000 milliseconds
        Timer1.Enabled = True 'Start Timer1 (my Timer)
    End Sub
    
    Private Sub Timer1_Timer()
        'This happens when the given Interval has passed (in this case, every second)
        Dim iMinuteNow As Integer
    
        iMinuteNow = Format(Now, "n") 
        If iMinuteNow <> iMinute Then  
            MsgBox "You are now in a new minute"
            iMinute = iMinuteNow
        End If
    End Sub
    
相关问题