Windows Develop Bookmark and Share   
 index > Windows Forms General > Question about different classes and EventHandler
 

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?

evilone  Tuesday, November 29, 2005 3:37 PM
What do you mean by "When I define this class in another class"? Can you post relevant parts of your code?

Mattias Sjögren  Tuesday, November 29, 2005 3:45 PM

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

Clock c = new Clock();

evilone  Tuesday, November 29, 2005 3:48 PM
It's still not at all clear exactly what the problem is.

Please provide a short but complete example which demonstrates the problem.

Jon

Jon Skeet  Tuesday, November 29, 2005 4:05 PM

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  Tuesday, November 29, 2005 4:20 PM
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.
Peter N Roth  Tuesday, November 29, 2005 11:31 PM

You can use google to search for other answers

Custom Search

More Threads

• RDP Audio Problems
• How to remove the ctrls at a time from a form or panel ?
• Writing Help for .Net windows forms application...
• Runtime Exceptions in Windows Form application
• C# ImageLayout
• Request name is valid, but no data of the request type found
• Timer
• DATAGIRVIEW
• how to see recently openfiles in file menubar
• Forms app with BackgroundWorker consumes 100% cpu while working.