Search code examples
c#.netgraphicscustom-controlsresizable

c# - clear surface when resizing


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.


Solution

  • Have you tried .Invalidate() to cause the form to redraw?