The OnPaint method calls a call for each child control

I have a UserControl (WinForms, .net 2.0) and I have 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 also drawn in all the child controls! I never read, it should be called for each child control, is there a solution?

+3
source share
2 answers

Since the child control is not valid for the parent, as a result, this method is launched for each of them.

, e (e , , ), .

+3

PaintEventArgs.ClipRectangle ? Control.ClientRectangle.

+6

Source: https://habr.com/ru/post/1704361/


All Articles