locked
make a Clock in Visual C# RRS feed

  • Question

  •  I want to make a Clock in Viuall C# and my Clock(digital) work fine and tell the right time but the problem is my dad want me to make a sperate tab where i would put a text box in witch i can change the time but the only probelm is i don't kniow how

    can somone pleez help me?!
    Tongue Tied

    anyone?

    pleez!
    Friday, August 12, 2005 7:50 AM

Answers

  • do the following

    First declare a private DateTime variable to hold the modified date

    private modifiedDate = DateTime.Now;

    Next add a Button next to your text box and in the Click event handler for the button add the following code

    // Given that the user enters correct date and time
    modifiedDate = DateTime.Parse( textBox1.Text );

    Now modify the Tick event handler for the Timmer like the following

    private void timer1_Tick(object sender, EventArgs e)
    {
      // Add the seconds interval you have specified in the Timmer.
      // I am adding 1 second
      modifiedDate = modifiedDate.AddSeconds(1);
      lblClock.Text = modifiedDate.ToLongTimeString();
    }

    you can remove the code in the Form's load event handler. This should fix it!

    Regards,
    Saurabh Nandu
    www.MasterCSharp.com
    www.AksTech.com
    Sunday, August 14, 2005 11:17 AM

All replies

  • Hi,

    Could you tell us on what approach did you take on creating that clock program of yours? In that case our response would be much clearer and precise...

     

     

    cheers,

    Paul June A. Domag

    • Proposed as answer by geogeniexy Thursday, March 6, 2014 7:54 PM
    Friday, August 12, 2005 8:35 AM
  • Well To make the Clock i put in a label and a timer control and then i put this code for the timer:

     DateTime dtCurrentTime = DateTime.Now;
                lblClock.Text = dtCurrentTime.ToLongTimeString();

    So now the time shows on the label

    But i want to be able to change the time by typing in a text box or something

    so the only problem is that i dont know how
    Friday, August 12, 2005 7:04 PM
  • the interface looks something like this
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace Timer_Example
    {
        public partial class fclsTimerExample : Form
        {
            public fclsTimerExample()
            {
                InitializeComponent();
            }

            private void fclsTimerExample_Load(object sender, EventArgs e)
            {
                DateTime dtCurrentTime = DateTime.Now;
                lblClock.Text = dtCurrentTime.ToLongTimeString();
                   
            }

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


            }

            private void label1_Click(object sender, EventArgs e)
            {

            }

            private void tabPage1_Click(object sender, EventArgs e)
            {

            }

            private void tmrDate_Tick(object sender, EventArgs e)
            {
              
            }

            private void textBox1_TextChanged(object sender, EventArgs e)
            {

            }
        }
    }
    Friday, August 12, 2005 7:44 PM
  • do the following

    First declare a private DateTime variable to hold the modified date

    private modifiedDate = DateTime.Now;

    Next add a Button next to your text box and in the Click event handler for the button add the following code

    // Given that the user enters correct date and time
    modifiedDate = DateTime.Parse( textBox1.Text );

    Now modify the Tick event handler for the Timmer like the following

    private void timer1_Tick(object sender, EventArgs e)
    {
      // Add the seconds interval you have specified in the Timmer.
      // I am adding 1 second
      modifiedDate = modifiedDate.AddSeconds(1);
      lblClock.Text = modifiedDate.ToLongTimeString();
    }

    you can remove the code in the Form's load event handler. This should fix it!

    Regards,
    Saurabh Nandu
    www.MasterCSharp.com
    www.AksTech.com
    Sunday, August 14, 2005 11:17 AM
  • how do i delcare the private datetime variable and where do i put it in the code?


    Monday, August 15, 2005 6:53 AM
  • put the variable just after the class decleration. the variable should be defined as

    ...
    // Class Definition
    private DateTime modifiedDate = DateTime.Now


    Regards,
    Saurabh Nandu
    Monday, August 15, 2005 3:39 PM
  • I don't know exactly what your dad wants, but there are a couple of issues with the approach taken by Saurubh that you should be aware of.

    1.  Changing the time, only changes the variable, it does not change your system's time.  If that was your intention, great!  If not, then you don't need a member variable, you need to set the system clock.  I looked briefly, but didn't find the way to do that.

    2.  You need to stop the timer whenever the user starts to update the field, otherwise their changes will get overwritten before they can complete them.  (Restart the timer after they update.  You should also give them a way to cancel the update.)

    3.  Whether or not you use a local variable to store the time, you shouldn't be adding a second every time the timer clicks.  It's inaccurate and you will lose time.  If you are updating the system time, you should just set the text according to System.DateTime.Now.  If you are not updating the system time, you should be storing the difference between the system time and your user input time.  Then display the time based on System.DateTime.Now + the difference.
    Monday, August 15, 2005 4:11 PM
  • yeah that was my intention, to change the time and only the varible not the system clock and thanks for repliying this really helped me
    Thursday, August 18, 2005 10:53 AM