I created an application in Winforms and use a custom control extended from Panel, that contain multiple line inside it and I have a button in my form. I need when user click on the button, color of lines in panel will be change.
I use Invalidate() method to refresh this panel for see changes.
private void button1_Click_2(object sender, EventArgs e)
{
if (MyPanel.mycolor2 == Color.Red)
{
MyPanel.mycolor2 = Color.Blue;
}
else
{
MyPanel.mycolor2 = Color.Red;
}
MyPanel.Invalidate();
}
My app work correctly. but when I clicked on button, for a moment my shape disappeared.
I tried release version of app, but my problem doesn’t resolved
>Solution :
- Enable Double-Buffering on your Panel.
class MyPanel : Panel
{
public new bool DoubleBuffered
{
get {
return base.DoubleBuffered;
}
set {
base.DoubleBuffered = true;
}
}
public MyPanel() :
base()
{
this.DoubleBuffered = true;
}
}
-
If you have several child controls on that panel you might want to call
SuspendLayout()andResumeLayout(). -
Use
Refresh()instead ofInvalidate()for an immediate update of your control. -
The real reason is probably the manual drawing of the lines. If you provide the code for that people can improve it or give advise.