Timer and Paint
-
Hi all. Please help me out with this. What i am doing is that, in a small form i am displaying the current time. To keep it updated, i am using a timer and calling Invalidate on the form, after every 1000 ms, to paint the surface clean for me. Now the problem is that, the time does not display at all. It sometimes appears, but for such a short duration that it is hardly visible. Here's what i am doing:
class MyClock { ..... public MyClock() { m_timer = new System.Windows.Forms.Timer(); m_timer.Interval = 1000; m_timer.Tick += new EventHandler(Start); m_timer.Start(); } private static void Start(Object o, EventArgs e) { m_timer.Stop(); m_form.Invalidate(true); m_time = DateTime.Now; string time = m_time.Hour + ":" + m_time.Minute + ":" + m_time.Second; Brush b = Brushes.White; m_gFormGraphic.DrawString(time, new Font("Verdana", 15), b, new PointF(20, 40)); m_timer.Start(); } ..... }
I tried adding handlers for Validating and Validated, where i set a boolean variable and display the time only when the form has been validated. But this doesnt work too. Any pointers will be highly appreciated. Thanks in advance. *** Who said nothing is impossible? I have been doing it for a long time *** -- modified at 13:56 Wednesday 29th March, 2006
-
Hi all. Please help me out with this. What i am doing is that, in a small form i am displaying the current time. To keep it updated, i am using a timer and calling Invalidate on the form, after every 1000 ms, to paint the surface clean for me. Now the problem is that, the time does not display at all. It sometimes appears, but for such a short duration that it is hardly visible. Here's what i am doing:
class MyClock { ..... public MyClock() { m_timer = new System.Windows.Forms.Timer(); m_timer.Interval = 1000; m_timer.Tick += new EventHandler(Start); m_timer.Start(); } private static void Start(Object o, EventArgs e) { m_timer.Stop(); m_form.Invalidate(true); m_time = DateTime.Now; string time = m_time.Hour + ":" + m_time.Minute + ":" + m_time.Second; Brush b = Brushes.White; m_gFormGraphic.DrawString(time, new Font("Verdana", 15), b, new PointF(20, 40)); m_timer.Start(); } ..... }
I tried adding handlers for Validating and Validated, where i set a boolean variable and display the time only when the form has been validated. But this doesnt work too. Any pointers will be highly appreciated. Thanks in advance. *** Who said nothing is impossible? I have been doing it for a long time *** -- modified at 13:56 Wednesday 29th March, 2006
I would recommend moving the painting to your form's
Paint
handler, which will (eventually) be invoked as a result of callingInvalidate()
or at any other time Windows deems it necessary to repaint the form. /ravi My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com -
Hi all. Please help me out with this. What i am doing is that, in a small form i am displaying the current time. To keep it updated, i am using a timer and calling Invalidate on the form, after every 1000 ms, to paint the surface clean for me. Now the problem is that, the time does not display at all. It sometimes appears, but for such a short duration that it is hardly visible. Here's what i am doing:
class MyClock { ..... public MyClock() { m_timer = new System.Windows.Forms.Timer(); m_timer.Interval = 1000; m_timer.Tick += new EventHandler(Start); m_timer.Start(); } private static void Start(Object o, EventArgs e) { m_timer.Stop(); m_form.Invalidate(true); m_time = DateTime.Now; string time = m_time.Hour + ":" + m_time.Minute + ":" + m_time.Second; Brush b = Brushes.White; m_gFormGraphic.DrawString(time, new Font("Verdana", 15), b, new PointF(20, 40)); m_timer.Start(); } ..... }
I tried adding handlers for Validating and Validated, where i set a boolean variable and display the time only when the form has been validated. But this doesnt work too. Any pointers will be highly appreciated. Thanks in advance. *** Who said nothing is impossible? I have been doing it for a long time *** -- modified at 13:56 Wednesday 29th March, 2006
CoolASL wrote:
It sometimes appears, but for such a short duration that it is hardly visible.
That's because you are not doing the drawing in the Paint event. Now you are just drawing it on the screen, and when the form is being redrawn because you invalidated it, the time that you drew is painted over. Why are you drawing the time as graphics anyway? Put the string in a label, and it will draw itself. When you change the Text property of the label, it will also invalidate itself so that it's redrawn. --- b { font-weight: normal; }
-
CoolASL wrote:
It sometimes appears, but for such a short duration that it is hardly visible.
That's because you are not doing the drawing in the Paint event. Now you are just drawing it on the screen, and when the form is being redrawn because you invalidated it, the time that you drew is painted over. Why are you drawing the time as graphics anyway? Put the string in a label, and it will draw itself. When you change the Text property of the label, it will also invalidate itself so that it's redrawn. --- b { font-weight: normal; }
Ok, please refresh me on this. I call Invalidate; that repaints my form. Then i use DrawString to draw my string on the form. Both are seperate entities... so whats the catch ???? Can you please brief me on what happening that i am not able to understand? Thanks. *** Who said nothing is impossible? I have been doing it for a long time ***
-
I would recommend moving the painting to your form's
Paint
handler, which will (eventually) be invoked as a result of callingInvalidate()
or at any other time Windows deems it necessary to repaint the form. /ravi My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com -
Ok, please refresh me on this. I call Invalidate; that repaints my form. Then i use DrawString to draw my string on the form. Both are seperate entities... so whats the catch ???? Can you please brief me on what happening that i am not able to understand? Thanks. *** Who said nothing is impossible? I have been doing it for a long time ***
No, the Invalidate call doesn't repaint the form. It only flags that the form needs repainting. The actual repaint can't be done until the method call has returned, so the system can call the Paint event for all controls in the form. --- b { font-weight: normal; }