I have two Panels stacked in Form, When one panel1 is showing,the panel2 is in background and If i call Panel2 using BringToFront() method to View the Panel2, The paint Event of the Panel2 is calling. So i want to prevent Panel2 from calling Paint Event to Disable Redrawing of the Panel.
Asked
Active
Viewed 1,243 times
0
-
1Have you tried achieving this through Panel1.Visible = false; instead? just my 2 cents, thanks! – ken lacoste Aug 16 '15 at 06:30
-
i tried with visible property also and it is the calling paint method. – userg1 Aug 16 '15 at 06:35
-
There are number of images in both panels, So if i swap between panels it is flickering even though i enabled double buffering in my Form. – userg1 Aug 16 '15 at 06:38
1 Answers
0
I think the paint method is a default event that has any regard to "display", hmm have you tried doing a workaround like creating a separate static flag for the purpose of preventing your paint events?
(again this is just a workaround)
Sample :
public bool displayed {get; set;}
private void Form1_Load(object sender, EventArgs e)
{
displayed = false;
}
private void Panel1_Paint(object sender, PaintEventArgs e)
{
if(!displayed) {
// do paint functions
displayed = true;
}
}
EDIT : Please try this one.
private void Form1_Load(object sender, EventArgs e)
{
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawRectangle(
new Pen(Color.Green, 3),
new Rectangle(10, 10, 50, 50));
//Drawing an ellipse
e.Graphics.FillEllipse(Brushes.Green, new Rectangle(60, 60, 100, 100));
//Drawing text
e.Graphics.DrawString("Text", new Font("Verdana", 14), new SolidBrush(Color.Green), 200, 200);
}
private void button1_Click_1(object sender, EventArgs e)
{
panel1.Visible = true;
panel2.Visible = false;
}
private void button2_Click_1(object sender, EventArgs e)
{
panel1.Visible = false;
panel2.Visible = true;
}
// code described below
public class MyDisplay : Panel
{
public MyDisplay()
{
this.DoubleBuffered = true;
}
}
As mentioned here Gets or sets a value indicating whether this control should redraw its surface using a secondary buffer to reduce or prevent flicker. This should now work regardless if SendToBack() BringToFront() or Visible set.

ken lacoste
- 894
- 8
- 22
-
-
would you mind sharing your form load and paint code? I'll try to polish this in my environment. – ken lacoste Aug 16 '15 at 07:20
-
Please try with simple code as you can. I simply want to prevent repaint of my control once loaded. – userg1 Aug 16 '15 at 07:37
-
-
then something is wrong with your code...that I cannot prove as I don't see it, in my solution, no flickering happens and the hide & show panel works without issue. – ken lacoste Aug 17 '15 at 06:03