I want a textbox on the form to show the current time. The code I have written follows. But it does not work and I don't know why :( Can someone please point out the error.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private System.Timers.Timer myTimer = new System.Timers.Timer();
private TextBox textBox2;
private System.ComponentModel.IContainer components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(84, 186);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 0;
this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
//
// Form1
//
this.ClientSize = new System.Drawing.Size(284, 264);
this.Controls.Add(this.textBox2);
this.Name = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
this.Load += new System.EventHandler(this.Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
myTimer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);
myTimer.Interval = 1000;
myTimer.Start();
}
public void DisplayTimeEvent( object source, ElapsedEventArgs e )
{
textBox2.Text = DateTime.Now.ToString();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
Comment