-2

I am using a Control. I have added two Panels in it as i would like to draw the multiple panels over a control. i overrided OnPaint() method for those two panels, but OnPaint() method of the first panel added to the control alone called, OnPaint() method not called for the second panel that i added to the control.

Note: I have used the below code to redraw the surface to avoid flicker issues. If i remove the below codes from my sample, OnPaint() method called for the second panel, but the element drawn in the second panel is not in Visual. (i.e,) Not displayed.

SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.Selectable |
                     ControlStyles.UserPaint |
                     ControlStyles.AllPaintingInWmPaint, true);

How can i achieve multiple panels drawing on a single control? Thanks in advance.

Code:

public Class VContainer : Panel

{

public CPanel CPanel;

public SPanel SPanel;

public VContainer()

{

this.CPanel = new CPanel();

this.SPanel = new Spanel();

this.Controls.Add(Cpanel);  **// first added Panel**

this.Controls.Add(SPanel);

}

protected override void OnMouseDown(MouseEventArgs e)

{

this.CPanel.Invalidate();

this.SPanel.Invalidate();

this.SPanel.Update();

}

}

public class CPanel : Panel

{

public CPanel()

{
// Used to redraw the surface to avoid flickering issues
SetStyle(ControlStyles.OptimizedDoubleBuffer |
                           ControlStyles.UserPaint |
                           ControlStyles.AllPaintingInWmPaint, true);

}

**// OnPaint() called since Cpanel is added first to the VContainer**

protected override void OnPaint(PaintEventArgs e)

{
e.Graphics.FillRectangle(Brushes.Red, new Rect(0,0,50,50));

base.OnPaint(e);

}

}

public class SPanel : Panel

{

public SPanel()

{
// Used to redraw the surface to avoid flickering issues
SetStyle(ControlStyles.OptimizedDoubleBuffer |
                           ControlStyles.UserPaint |
                           ControlStyles.AllPaintingInWmPaint, true);
}

**// OnPaint() method is not called while invalidating the Panel since the Spanel is added as second control to VContainer**

protected override void OnPaint(PaintEventArgs e)

{
e.Graphics.FillRectangle(Brushes.Green, new Rect(0,0,50,50));

base.OnPaint(e);

}

}

1 Answers1

0

Venkatesan,

Your two controls CPanel and SPanel are at the same position.

So one overlaps the other and Winforms doesn't draw the one under.

Change the positions and sizes of the controls, and you 'll see something :

public VContainer()
{
    this.CPanel = new CPanel();
    CPanel.Top = 0;
    CPanel.Left = 0;
    CPanel.Width = 50;
    CPanel.Height = 50;
    this.Controls.Add(CPanel);  // first added Panel**


    this.SPanel = new SPanel();
    SPanel.Top = 50;
    SPanel.Left = 50;
    SPanel.Width = 50;
    SPanel.Height = 50; 
    this.Controls.Add(SPanel);
}

By the way, change the order of the instructions of the OnPaint method.
Because if base class makes some drawing, it will cover what you do :

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    e.Graphics.FillRectangle(Brushes.Red, new Rectangle(0, 0, 50, 50));
}

Full source to working solution : http://1drv.ms/1NtIttW

Regards

Emmanuel DURIN
  • 4,803
  • 2
  • 28
  • 53