BLOG.CSHARPHELPER.COM: Redraw a picture whenever the form resizes in C#
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); }
Comments