Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnPaint method is being call for every child control

I've a UserControl (WinForms, .net 2.0), and I've this:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    var rect = e.ClipRectangle;
    var pen = new Pen(Brushes.LightGray, 1);

    e.Graphics.DrawRectangle(pen, rect);
}

I basically want to draw a border on the UserControl, but the rectangle is being draw in all the child controls too! I never read it should be called for every child control, is there a solution?

like image 899
Diego Jancic Avatar asked Dec 09 '25 12:12

Diego Jancic


1 Answers

Why are you using PaintEventArgs.ClipRectangle to determine the bounds of the rectangle? Try using Control.ClientRectangle instead.

like image 127
Shog9 Avatar answered Dec 11 '25 01:12

Shog9



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!