mike663
mike663

Reputation: 623

painted form contents won't stay painted

I've tried setting ResizeRedraw, but it was not causing paint events on resize, so I began invalidating inside myForm_Resize. Now when I resize, I see that my background is being repainted, but as soon as I stop resizing, my control is being repainted with its background color. What am I doing wrong?

private void pbox_Paint(object sender, PaintEventArgs e) {
    Rectangle boardRect = pbox.ClientRectangle;
    using (Graphics g = pbox.CreateGraphics()) {
        g.FillRectangle(Brushes.Orange, boardRect);
    }
}

private void myForm_Resize(object sender, EventArgs e) {
    this.Invalidate(true);
}

Upvotes: 0

Views: 147

Answers (1)

Nikola Davidovic
Nikola Davidovic

Reputation: 8656

Note that there is no point in creating Graphics object in Paint event handler. You should use existing Graphics object from the PaintEventArgs. Change your code like this, it should work now:

private void pbox_Paint(object sender, PaintEventArgs e) 
{
      Rectangle boardRect = pbox.ClientRectangle;
      e.Graphics.FillRectangle(Brushes.Orange, boardRect);
}

Upvotes: 3

Related Questions