c# - clear surface when resizing

Posted by dhh on Stack Overflow See other posts from Stack Overflow or by dhh
Published on 2010-05-13T14:56:58Z Indexed on 2010/05/13 15:24 UTC
Read the original article Hit count: 278

Filed under:
|
|
|
|

Hello,

I'm trying to build my own custom control for a windows forms application in C#.Net. Currently I paint some rectangles and other graphic elements using the paint event. When I now resize the app form to fit the desktop size, all elements are repainted (which is exactly the behaviour I need) but the old one's are shown in the background.

Here's what I'm doing by now:

Pen penDefaultBorder = new Pen(Color.Wheat, 1);
int margin = 5;
private void CustomControl_Paint(object sender, PaintEventArgs e) {
    CustomControl calendar = (CustomControl)sender;
    Graphics graphics = e.Graphics;
    graphics.Clear(Color.WhiteSmoke);

    graphics.DrawRectangle(penDefaultBorder, margin, margin, calendar.Width - margin * 2, calendar.Height - margin * 2);
    //...
}

Neither the graphics.Clear, nor adding a graphics.FillRectangle(...) will hide the old rectangle from the surface.

Ideas? Thank you all.

© Stack Overflow or respective owner

Related posts about c#

Related posts about custom-controls