Reputation: 33
I have a form with a label in it called labelTime
.
In another class called TimeCalculate
I have a Timer
with Timer_tick
event handler.
In this class I also have a function GetTime()
that returns the time in string format.
I want this string to appear in labelTime
for every Timer_tick
.
Is there a way to achieve that?
public void MyTimer(Label o_TimeLabel)
{
Timer Clock = new Timer();
Clock.Tag = o_TimeLabel.Text;
Clock.Interval = 1000; Clock.Start();
Clock.Tick += new EventHandler(Timer_Tick);
}
private void Timer_Tick(object sender, EventArgs eArgs)
{
if (sender == Clock)
{
//LabelTime.Text = GetTime(); <-- I want this to work!
}
}
Upvotes: 3
Views: 19393
Reputation:
The Timer
and Timer_Tick
event do not need to be in the same class as your Label
, you can create a simple custom event to publish/subscribe to your Timer_Tick
event
.
Your TimeCalculate
class:
namespace StackOverflow.WinForms
{
using System;
using System.Windows.Forms;
public class TimeCalculate
{
private Timer timer;
private string theTime;
public string TheTime
{
get
{
return theTime;
}
set
{
theTime = value;
OnTheTimeChanged(this.theTime);
}
}
public TimeCalculate()
{
timer = new Timer();
timer.Tick += new EventHandler(Timer_Tick);
timer.Interval = 1000;
timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
TheTime = DateTime.UtcNow.ToString("dd/mm/yyyy HH:mm:ss");
}
public delegate void TimerTickHandler(string newTime);
public event TimerTickHandler TheTimeChanged;
protected void OnTheTimeChanged(string newTime)
{
if (TheTimeChanged != null)
{
TheTimeChanged(newTime);
}
}
}
}
The extremely simplified example above shows how you can use a delegate
and event
to publish
a notification when the Timer_Tick
Timer
object event fires.
Any objects that require notification when the Timer_Tick
event fires (I.E. your time is updated) need only to subscribe
to your custom event publisher:
namespace StackOverflow.WinForms
{
using System.Windows.Forms;
public partial class Form1 : Form
{
private TimeCalculate timeCalculate;
public Form1()
{
InitializeComponent();
this.timeCalculate = new TimeCalculate();
this.timeCalculate.TheTimeChanged += new TimeCalculate.TimerTickHandler(TimeHasChanged);
}
protected void TimeHasChanged(string newTime)
{
this.txtTheTime.Text = newTime;
}
}
}
We create an instance of the TimeCalcualte
class before subscribing to the TimerTickHandler
event specifying a method (TimeHasChanged
) to handle the notification. Note that txtTheTime
is the name I gave a TextBox
on my form.
Upvotes: 1
Reputation: 1055
You need the Timer and its event in the same form as the Label
EDIT
Since you have done it that way, you need to;
Declare the Timer object as an instance variable, outside of the constructor, initialise it in the constructor.
Don't have to worry about testing 'sender == Clock'.
And have a Label instance object in this class as well, setting it to the Label you pass as a parameter in the constructor.
Timer Clock;
Label LabelTime;
public void MyTimer(Label o_TimeLabel)
{
LabelTime = o_TimeLabel;
Clock = new Timer();
Clock.Tag = o_TimeLabel.Text;
Clock.Interval = 1000;
Clock.Start();
Clock.Tick += new EventHandler(Timer_Tick);
}
private void Timer_Tick(object sender, EventArgs eArgs)
{
LabelTime.Text = GetTime(); // For your custom time
}
Upvotes: 1
Reputation: 18843
Rebecca in your Time_Tick Event you could do the following
private void Timer_Tick(object sender, EventArgs e)
{
lblTime.Text = DateTime.Now.ToString("hh:mm:ss");
}
Upvotes: 1