Click here to Skip to main content
14,739,403 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi dears
I tried to make an simple alarm
I have a timer, a datetimepicker and a button

the code is here:

private void timer1_Tick(object sender, EventArgs e)
        {
            label3.Text = DateTime.Now.ToLongTimeString();

            DateTime dte = DateTime.Now;
            DateTime dts = dateTimePicker1.Value;
            TimeSpan ts = new TimeSpan();
            ts = dts - dte;
            int x = ts.Milliseconds;
            tsec++;
            if (tsec == x)
                MessageBox.Show("gsd");
        }

        private void button1_Click(object sender, EventArgs e)
        {

            timer1.Enabled = true;
            timer1.Interval = 1000;
            timer1.Start();
            
        }


excuse me im very new on c#

what is wrong here?
thanks
Posted
Updated 10-Aug-15 23:48pm
v2
Comments
sreeyush sudhakaran 11-Aug-15 4:53am
   
what is your error?
amir.nazarizadeh 11-Aug-15 4:55am
   
I set the datetimepicker to a specific time but when time of system being equals to that, messagebox not shows!
sreeyush sudhakaran 11-Aug-15 5:01am
   
when u set like this ts = dts - dte; dte will be always greater than dts so you will get a negative values here

Don't do it like that!
Try this:
private bool waitingTimer = true;
private void timer1_Tick(object sender, EventArgs e)
    {
    DateTime now = DateTime.Now;
    label3.Text = now.ToLongTimeString();
    
    if (waitingTimer && now > dateTimePicker1.Value)
        {
        waitingTimer = false;
        MessageBox.Show("gsd");
        }
    }


The problem with your method is that it expects the timer to happen all the time, and to be accurate to the millisecond - but if you don't start the timer when the current milliseconds is zero, it will never match!
   
Comments
amir.nazarizadeh 11-Aug-15 5:13am
   
thanks and thanks for your answer but, with your code when the app starts, messagebox shows the string but if I set datetimepicker to a specific time nothing will happen!
thanks for your help :)
Pascal-78 11-Aug-15 5:27am
   
Reset the waitingTimer to true each time the user change the dateTimePicker1 value to reactivate the alarm.
amir.nazarizadeh 11-Aug-15 5:31am
   
that works! thanks and thanks for your solution :)
OriginalGriff 11-Aug-15 5:32am
   
Yes - the waitingTimer variable is there to ensure it happens once, and once only.
Use the DateTimePicker.ValueChanged event to set it back to true, and it will fire next time.

(and the greater than in the code was a paste artifact - I'll fix it.)
amir.nazarizadeh 11-Aug-15 5:31am
   
that works! thanks and thanks for your solution :)
OriginalGriff 11-Aug-15 5:33am
   
You're welcome!
You can also re-write your code like this :

private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

private void timer1_Tick(object sender, EventArgs e)
{
   label3.Text = DateTime.Now.ToLongTimeString();

   long dte = (long) (DateTime.UtcNow - UnixEpoch).TotalMilliseconds;
   long dts = (long) (dateTimePicker1.Value - UnixEpoch).TotalMilliseconds;

    if (dte == dts)
        MessageBox.Show("gsd");

}
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900