Question about different classes and EventHandler
Hello again everyone!
I'm quite new in C#. Yesterday I made a Clock class - it's using a System.Windows.Forms.Timer and there's a EventHandler timer.Tick. When I define this class in another class. It won't update the clock. Why it is like that and how can I change it?
[361 byte] By [
evilone] at [2007-12-17]
class Clock {
private int timerInterval;
private Timer clockTimer;
private string longTimeString;
private DateTime timeNow;
public Clock(int timerInterval) {
this.timerInterval = timerInterval;
timeNow = new DateTime();
clockTimer = new Timer();
clockTimer.Interval = timerInterval;
clockTimer.Tick += new EventHandler(UpdateTimeDisplay);
}
public void Start() {
clockTimer.Start();
}
public void Stop() {
clockTimer.Stop();
clockTimer.Dispose();
}
public string TimeDisplay() {
return longTimeString;
}
private void UpdateTimeDisplay(object sender, EventArgs e) {
timeNow = DateTime.Now;
longTimeString = timeNow.ToLongTimeString();
}
}
And then I try add this class to Main Form class, like Clock c = new Clock(1000).
And then I hope it updates the Label lblTimeDisplay using the TimeDisplay() method - lblTimeDisplay.Text = c.TimeDisplay(), but it doesn't.