What I need is to have a timer fire an event handler (say every second) which is in another class. This will be a small part of a Windows form program.
I have tried using a delegate to "call" an event handler, but I keep getting syntax errors. Can somebody steer me in the correct direction with a simple code example?
The code below is my start, the commented portion works fine but I want the event to fire when a 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 to 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" is incorrect, invalid argument
}
}
}
}