Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I am working on Windows Application.
In a form I dragged one label and one timer control.
I have written the following code:-
 
On form load event:-
Timer1.Start(); // Timer in initialized.
private void timer1_Tick(object sender, EventArgs e)
        {
            bool status = false;
            label1.Text = DateTime.Now.ToLongTimeString();
            DateTime time = Convert.ToDateTime("10:05:00 PM");
            if (DateTime.Now.ToLongTimeString() == time.ToLongTimeString())
            {
                MessageBox.Show("Perfect Code.", "Timer Control", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
I was running program at 10 pm so I entered 10 pm. Enter the timing after the current time.
 
Error in this program is that when the user time is equal to system time Message Box is shown 9 times. Help me sort out this.
 
Thanks in Advance....
Posted 17-Aug-12 9:04am
Ank_ush2.8K
Edited 17-Aug-12 9:06am
v2
Comments
Sergey Alexandrovich Kryukov at 17-Aug-12 15:08pm
   
Where is Interval and other related to timer code?
--SA
Wes Aday at 17-Aug-12 15:08pm
   
And how long is your timer interval?
Sergey Alexandrovich Kryukov at 17-Aug-12 15:10pm
   
Bad code. Working with strings instead of time, calling a MessageBox from the time tick event handler. But first, what do you want to achieve?
--SA
learner'sbug at 17-Aug-12 15:14pm
   
Its showing it 9 times because default interval is 100 milliseconds and your precision point is second but again what are you trying to do ?
Ank_ush at 17-Aug-12 15:27pm
   
yes you r right
I changed it to 1000 and its done.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I set the time Interval of timer control to 1000, now it's showing only once.
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 17 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100