Redraw a picture whenever the form resizes in C#

When a form grows larger, parts of it are exposed and the form raises a Paint event so the form can redraw. Unfortunately the event handler can only draw on the newly exposed areas. The Paint event also is not raised when the form shrinks because no new areas are exposed. Neither of these is a problem if you are drawing something simple but it can produce strange results if the drawing sizes itself to fit the form (see the picture).

To handle this, use code to set the form's ResizeRedraw property to true. Then whenever the form resizes, it raises a Paint event. (Strangely this property isn't visible in the Properties window at design time so you have to set it in code.)

This example uses the following code to set ResizeRedraw to true or false when you check and uncheck a CheckBox so you can compare the program's behavior.

// Turn ResizeRedraw on or off.
private void chkResizeRedraw_CheckedChanged(object sender, EventArgs e)
{
this.ResizeRedraw = (chkResizeRedraw.Checked);
}

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.