Code:
Tri clana klase:
Label l;
Button b;
Timer t;
Konstruktor: // dodati posle 'InitializeComponent' metoda
l = new Label();
l.Location = new Point(10, 10);
l.Text = "0";
Controls.Add(l);
b = new Button();
b.Location = new Point(10, 40);
b.Text = "Start";
b.Click += new EventHandler(ButtonClick);
Controls.Add(b);
t = new Timer();
t.Interval = 1000; // interval u milisekundama
t.Tick += new EventHandler(TimerTick); // ovaj dogadjaj se desi svakih 1000 ms
Metodi:
void ButtonClick(object s, EventArgs e)
{
if (b.Text == "Start") {
t.Start();
b.Text = "Stop";
} else {
t.Stop();
b.Text = "Start";
}
}
void TimerTick(object s, EventArgs e)
{
int i = Convert.ToInt32(l.Text);
i++;
l.Text = Convert.ToString(i);
}