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. how to call onPaint from Timer

how to call onPaint from Timer

Scheduled Pinned Locked Moved C#
graphicsquestioncsharptutorialannouncement
4 Posts 3 Posters 3 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.
  • B Offline
    B Offline
    bimbambumbum
    wrote on last edited by
    #1

    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 drawn

        public 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
    
        }
    
    A 1 Reply Last reply
    0
    • B bimbambumbum

      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 drawn

          public 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
      
          }
      
      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      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.

      B 1 Reply Last reply
      0
      • A Abhinav S

        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.

        B Offline
        B Offline
        bimbambumbum
        wrote on last edited by
        #3

        It sure does... Thanks this is great Tom :)

        T 1 Reply Last reply
        0
        • B bimbambumbum

          It sure does... Thanks this is great Tom :)

          T Offline
          T Offline
          Thomas Stockwell
          wrote on last edited by
          #4

          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

          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