Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
i am really new in this and really need instance answer.
i am working on a application about a simple clock(which is actually a H.W and due for next 4 hours),it has setting time&Date button, Alarm, Stopwatch, Reminder.

after clicking the Set time&Date button another form will be open and get the data and transfer it to a label in form one. till here all is good but the problem is that the new time that user had set will not tick everything is frozen.
here is my code

C#
//in form1

private void button1_Click(object sender, EventArgs e)
        {
            ClassAll obj = new ClassAll();
            Form2 f2 = new Form2();
            f2.ShowDialog();

            label1.Text = obj.SetTimeHour(f2.Hour);
            label2.Text = obj.SetTimeMin(f2.Min);
            label3.Text = obj.SetTimeSecond(f2.Sec);
            timer1.start();
        }

//timer1

{
//something must be here that i don't know
//something that keeps the time ticking
//and checks that doesn't over flow the limits
}


// ClassAll
public class ClassAll

public string SetTimeSecond(int s)
        {
            return string.Format("{0:D2}", ((s > 0 && s < 60) ? s : 0) );
        }

        //=============================================================
        public string SetTimeMin(int min)
        {
            return string.Format("{0:D2}", ((min > 0 && min < 60) ? min : 0));
        }

        //=============================================================
        public string SetTimeHour(int h)
        {
            return string.Format("{0:D2}", ((h > 0 && h < 24) ? h : 0) );
        }

thank you all
Posted

1 solution

First off, don't create new timers and don't start / stop the existing one. If you are going to use a timer then set it for a suitable interval (a second perhaps) and leave it running.
Then, in the Tick event compare the Alarm time against teh current time:
C#
DateTime now = DateTime.Now;
...
if (alarmTime >= now)
   {
   // Clear alarm, and indicate to user
   ...
   }
You can keep a list of alarms, and process them all with a single timer that way.

If you want to update a label to show the current time, you just add the appropriate code to teh same Tick handler:
C#
label1.Text = now.ToString("hh");
label2.Text = now.ToString("mm");
label3.Text = now.ToString("ss");
 
Share this answer
 

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