how to call onPaint from Timer
-
Hi I'm trying to work my way into the world of .NET programming so this question might be a no-brainer. I'm trying to make a timer-control redrawing GDI graphics so that the GUI gets updated at regular time intervals. The overrided OnPaint() is currently doing the drawing and it should be called from the timer_tick() method. Please take a look at the code below. I looked for an onPaint() redraw/update method but couldn't find such one. Can someone please give me a hint? Thanks Tom
public partial class Form1 : Form
{
Timer timer = new Timer();
Graphics g;
int x2 = 100; // the end location of the line being drawn
int y2 = 100; // the end location of the line being drawnpublic Form1() { InitializeComponent(); timer.Tick += new EventHandler(timer\_tick); timer.Interval = 1000; // update graphics every sec!! timer.Enabled = true; timer.Start(); } void timer\_tick(object sender, EventArgs e) { //MessageBox.Show("Tick"); // Alert the user } private void Form1\_Paint(object sender, PaintEventArgs e) { } protected override void OnPaint(PaintEventArgs pe) { g = pe.Graphics; // Draw coordinate system Pen pn = new Pen(Color.Black, 1); Point pt1 = new Point(0, 0); Point pt2 = new Point(x2, y2++); g.DrawLine(pn, pt1, pt2); if (y2 > 300) y2 = 100; // increment y-coordinate to see that onPaint gets called from timer }
-
Hi I'm trying to work my way into the world of .NET programming so this question might be a no-brainer. I'm trying to make a timer-control redrawing GDI graphics so that the GUI gets updated at regular time intervals. The overrided OnPaint() is currently doing the drawing and it should be called from the timer_tick() method. Please take a look at the code below. I looked for an onPaint() redraw/update method but couldn't find such one. Can someone please give me a hint? Thanks Tom
public partial class Form1 : Form
{
Timer timer = new Timer();
Graphics g;
int x2 = 100; // the end location of the line being drawn
int y2 = 100; // the end location of the line being drawnpublic Form1() { InitializeComponent(); timer.Tick += new EventHandler(timer\_tick); timer.Interval = 1000; // update graphics every sec!! timer.Enabled = true; timer.Start(); } void timer\_tick(object sender, EventArgs e) { //MessageBox.Show("Tick"); // Alert the user } private void Form1\_Paint(object sender, PaintEventArgs e) { } protected override void OnPaint(PaintEventArgs pe) { g = pe.Graphics; // Draw coordinate system Pen pn = new Pen(Color.Black, 1); Point pt1 = new Point(0, 0); Point pt2 = new Point(x2, y2++); g.DrawLine(pn, pt1, pt2); if (y2 > 300) y2 = 100; // increment y-coordinate to see that onPaint gets called from timer }
-
Does
this.Refresh();
help?The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.
It sure does... Thanks this is great Tom :)
-
It sure does... Thanks this is great Tom :)
this.invalidate() works as well
Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my Blog