Results 1 to 6 of 6

Thread: Displaying Current Time in label...

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    28

    Displaying Current Time in label...

    ok i did a search and cant find any C# solution. i know i need to create a timer and put my code under that event but just not sure what to put there. here is what i tried and it didnt work

    Code:
    		private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    		{
    			lblTime.Text = System.DateTime.Now.ToShortTimeString();
    		}
    i must be totally missing something, if anyone could help me out i'd be greatly appricative

  2. #2
    Hyperactive Member Sgt-Peppa's Avatar
    Join Date
    Mar 2003
    Location
    Munich - Germany
    Posts
    476

    Re: Displaying Current Time in label...

    Did you enable the Timer somewhere? What hapens if you start the app?
    Keep Smiling - even if its hard
    Frankie Says Relax, wossname Says Yeah!
    wossname:--Currently I'm wearing a gimp suit and a parachute.
    C# - Base64 Blog

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    28

    Re: Displaying Current Time in label...

    not sure about enabling it, this is the first time i have used a timer... if i run the program i get no errors, it just doesnt do what i want it to

    oh i just saw the enabled property, that is set to true if thats what u are referring to.

  4. #4
    Hyperactive Member Sgt-Peppa's Avatar
    Join Date
    Mar 2003
    Location
    Munich - Germany
    Posts
    476

    Re: Displaying Current Time in label...

    This works for me:
    Code:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    
    namespace ShowTime
    {
    	/// <summary>
    	/// Summary description for Form1.
    	/// </summary>
    	public class Form1 : System.Windows.Forms.Form
    	{
    		private System.Windows.Forms.Label label1;
    		private System.Windows.Forms.Timer timer1;
    		private System.ComponentModel.IContainer components;
    
    		public Form1()
    		{
    			//
    			// Required for Windows Form Designer support
    			//
    			InitializeComponent();
    
    			//
    			// TODO: Add any constructor code after InitializeComponent call
    			//
    		}
    
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if (components != null) 
    				{
    					components.Dispose();
    				}
    			}
    			base.Dispose( disposing );
    		}
    
    		#region Windows Form Designer generated code
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		private void InitializeComponent()
    		{
    			this.components = new System.ComponentModel.Container();
    			this.label1 = new System.Windows.Forms.Label();
    			this.timer1 = new System.Windows.Forms.Timer(this.components);
    			this.SuspendLayout();
    			// 
    			// label1
    			// 
    			this.label1.Location = new System.Drawing.Point(8, 16);
    			this.label1.Name = "label1";
    			this.label1.TabIndex = 0;
    			this.label1.Text = "label1";
    			// 
    			// timer1
    			// 
    			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    			// 
    			// Form1
    			// 
    			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    			this.ClientSize = new System.Drawing.Size(292, 266);
    			this.Controls.Add(this.label1);
    			this.Name = "Form1";
    			this.Text = "Form1";
    			this.Load += new System.EventHandler(this.Form1_Load);
    			this.ResumeLayout(false);
    
    		}
    		#endregion
    
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main() 
    		{
    			Application.Run(new Form1());
    		}
    
    		private void Form1_Load(object sender, System.EventArgs e)
    		{
    			timer1.Interval = 1000;
    			timer1.Enabled = true;
    		}
    
    		private void timer1_Tick(object sender, System.EventArgs e)
    		{
    			label1.Text = System.DateTime.Now.ToShortTimeString();
    		}
    	}
    }
    Keep Smiling - even if its hard
    Frankie Says Relax, wossname Says Yeah!
    wossname:--Currently I'm wearing a gimp suit and a parachute.
    C# - Base64 Blog

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Displaying Current Time in label...

    You should always read at least the class topic and the member listing topic in the help documentation for any new class before using it. Here's a quote from the help topic for the System.Timers.Timer class:
    Note When AutoReset is set to false, the Timer raises the Elapsed event only once, after the first Interval has elapsed. To keep raising the Elapsed event on the Interval, set AutoReset to true.
    The default value for the AutoReset property is False.

    Having said that, you should probably use a System.Windows.Forms.Timer instead of a System.Timers.Timer. If you're using VS.NET 2003 (please specify in future; just because you don't think it's important doesn't mean it isn't) then the System.Windows.Forms.Timer is on the Windows Forms tab of the Toolbox while the System.Timers.Timer is on the Components tab. Below is a link to an article that compares the three types of Timers.

    http://msdn.microsoft.com/msdnmag/is...T/default.aspx
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    28

    Re: Displaying Current Time in label...

    Hey guys, i just got back to work and read ur posts. ill check all that info out. thx alot for the time guyz. ill post back with my results.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width