Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Timer and Paint

Timer and Paint

Scheduled Pinned Locked Moved C#
helpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CoolASL
    wrote on last edited by
    #1

    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

    R G 2 Replies Last reply
    0
    • C CoolASL

      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

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      I would recommend moving the painting to your form's Paint handler, which will (eventually) be invoked as a result of calling Invalidate() 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

      C 1 Reply Last reply
      0
      • C CoolASL

        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

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        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; }

        C 1 Reply Last reply
        0
        • G Guffa

          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; }

          C Offline
          C Offline
          CoolASL
          wrote on last edited by
          #4

          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 ***

          G 1 Reply Last reply
          0
          • R Ravi Bhavnani

            I would recommend moving the painting to your form's Paint handler, which will (eventually) be invoked as a result of calling Invalidate() 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

            C Offline
            C Offline
            CoolASL
            wrote on last edited by
            #5

            Hey, thanks buddy... I'll try that and let you know. Thanks a lot. *** Who said nothing is impossible? I have been doing it for a long time ***

            1 Reply Last reply
            0
            • C CoolASL

              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 ***

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              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; }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups