Search code examples
c#winformspanelonpaint

windows.forms.panel calling onPaint twice


I have a panel with multiple panels inside of it. I have overridden OnPaint in the master panel to the following:

    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics graph = e.Graphics;
        graph.Clear(Color.Black);
        InvokePaintBackground(this, e);

        graph.ScaleTransform(scale, scale);

        foreach (childPanel child in childPanels)
        {
            child.onPaint(this, e);
        }

        graph.ResetTransform();
    }

The problem I have is that the onPaint function of the first control (control in spot 0) is being called twice so I am getting two versions of the child panel, one with scaling, and one without. The second onPaint seems to be called by the child control itself.

How do I keep it from doing this?


Solution

  • That's because all Control objects do their own painting and the method is called automatically by Windows. The solution is to not rely on this sort of functionality at all - get rid of the panels, or set Visible to false.