C# Event Handler for a Timer Object -
what need have timer fire event handler (say every second) in class. small part of windows form program.
i have tried using delegate "call" event handler, keep getting syntax errors. can steer me in correct direction simple code example?
the code below start, commented portion works fine want event fire when windows timer fires.
namespace windowsformsapplication3 { public partial class form1 : form { public form1() { initializecomponent(); } public event timerhandler tick; public eventargs e = null; public delegate void timerhandler(timer t, eventargs e); public class timer { public event timerhandler tick; public eventargs e = null; public delegate void timerhandler(timer t, eventargs e); } public class listener { public static int ticker = 0; public void subscribe(timer t) { t.tick += new timer.timerhandler(heardtick); } private void heardtick(timer t, eventargs e) { //lbltimer.text = ticker.tostring(); //don't know how change forms control ticker++; } } private void btnstart_click_1(object sender, eventargs e) { timer t = new timer(); listener l = new listener(); l.subscribe(t); //t.start(); } public void timer1_tick(object sender, eventargs e) { if (tick != null) { tick(this, e); // "this" incorrect, invalid argument } } } }
is other class static? here example each:
//static class timer1.tick += yourclass.dostuff; //non-static class yourclass myinstance = new yourclass(); timer1.tick += myinstance.dostuff;
just put code in constructor of form.
Comments
Post a Comment