Tech Off Thread

4 posts

Clock

Back to Forum: Tech Off
  • User profile image
    SEP2007

    In Vb can anyone help with how to create a form that has a clock in it. So I can use it to tell the time.

    Do I need to create a thread when the form loads ?



    Or is there a clock obect with a tick event that I can use ?
    Is this the right approach ?


  • User profile image
    jsampsonPC

    Drag a label and a Timer onto your form, and double click the form itself. Make sure you start the timer in the Constructor of the form, as seen in line 6. Then, in the Tick event of the timer, update the label.Text property to the current time, as seen in line 11.

    Hope that helps.
      .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: Consolas, "Courier New", Courier, Monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

       1:      public partial class Form1 : Form
       2:      {
       3:          public Form1()
       4:          {
       5:              InitializeComponent();
       6:              timer1.Start();
       7:          }
       8:   
       9:          private void timer1_Tick(object sender, EventArgs e)
      10:          {
      11:              label1.Text = DateTime.Now.ToString();
      12:          }
      13:      }

  • User profile image
    SEP2007

    Brilliant, thanks.

  • User profile image
    SEP2007

    It worked

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.