计时器 - 在特定时间过后显示消息

时间:2017-03-15 13:26:19

标签: c# visual-studio-2013

我的表格上有一个计时器,它显示当前时间(基于我的系统)我想要做的是例如当前时间是“下午6:22”所以当它到达“下午6:24” “它应该显示一条消息”你必须提交报告“我该怎么办?这是我的代码。

 private void timer1_Tick(object sender, EventArgs e)
    {
        label1.Text = DateTime.Now.ToString("MM-dd-yyyy hh:mm:ss tt");
    }

这只是显示当前时间。有人可以帮我解决这个“消息框”吗?

2 个答案:

答案 0 :(得分:0)

timer1的时间间隔是多少?可能是1000毫秒,即1秒。假设1秒的时间间隔,添加一个条件

private DateTime showOnDate = DateTime.Today;
private void timer1_Tick(object sender, EventArgs e)
{
    label1.Text = DateTime.Now.ToString("MM-dd-yyyy hh:mm:ss tt");
    if (DateTime.Now.Hour == 18 && DateTime.Now.Minute == 24 && showOnDate == DateTime.Today)
    {
        showOnDate = DateTime.Today.AddDays(1);
        MessageBox.Show("You have to submit a report");
    }
}

答案 1 :(得分:0)

int minutes = Convert.ToDateTime("01/02/2017 06:24").Substract(Convert.ToDateTime("01/01/2017 06:22")).TotalMinutes;

if(minutes >2)
{
    MessageBox.Show("2 minutes has expired);
}