tags:

views:

60

answers:

1

I have a simple Form such as the following:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace PaintTest
{
   class PaintTest : Form
   {
      int _counter = 0;

      [STAThread]
      static void Main()
      {
         Application.Run(new PaintTest());
      }

      protected override void OnPaint(PaintEventArgs e)
      {
         e.Graphics.DrawString(_counter.ToString(), new Font(FontFamily.GenericSerif, 10.0f), Brushes.Blue, 10.0f, 10.0f);
         _counter++;
      }
   }
}

When the window is resized, the counter doesn't appear to update on the screen, even though OnPaint() is getting called and the counter incremented. How do I make the Form repaint itself as the window is resized?

+1  A: 

Add the following to your class, e.g. in its constructor:

SetStyle(ControlStyles.ResizeRedraw, true);

saw-lau
If the form has an InitializeComponent() call, would you set that style before or after it? or doesn't it matter?
Svish
that was btw, one fast answer to your own question...
Svish
Svish, I've just tested it and it doesn't matter. Re: the fast answer - I'd just worked it out after a frustrating morning and wanted to share the love! :-)
saw-lau