i have form label in called labeltime. in class called timecalculate have timer timer_tick event handler. in class have function gettime() returns time in string format. want string appear in labeltime every timer_tick. there way 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(); <-- want work! } }
the timer , timer_tick event not need in same class label, can create simple custom event publish/subscribe 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 { { 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 can use delegate , event publish notification when timer_tick timer object event fires.
any objects require notification when timer_tick event fires (i.e. time updated) need subscribe 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 instance of timecalcualte class before subscribing timertickhandler event specifying method (timehaschanged) handle notification. note txtthetime name gave textbox on form.
Comments
Post a Comment