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]
# 1
What do you mean by "When I define this class in another class"? Can you post relevant parts of your code?

MattiasSj?gren at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2

I made a Clock class, now I want to use it in Main form class -

Clock c = new Clock();

evilone at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
It's still not at all clear exactly what the problem is.
Please provide a short but complete example which demonstrates the problem.
Jon

JonSkeet at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4

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.

evilone at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5
My guess is that the update is being performed but your form is not being updated. Look in Help for the Invalidate method, which tells Windows that it needs to redraw your control.
PeterNRoth at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...