Timer - Show a message after a specific time has passed

大憨熊 提交于 2020-01-26 03:48:25

问题


I have got a timer on my form like and it shows the current time (based on my system) what i want to do is for example current time is "6:22 PM" so when it reaches "6:24 PM" it should show a Message as "You have to submit a report" how do i do? Here is my code.

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

This just shows the current time.Can someone help me on this "message box"?


回答1:


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);
}



回答2:


what is time interval of timer1 ? Probably its 1000 ms i.e. 1 sec. Assuming 1 sec time interval, add a condition

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");
    }
}


来源:https://stackoverflow.com/questions/42811160/timer-show-a-message-after-a-specific-time-has-passed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!