问题
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