Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to apply changes to a shape without any interruption in C#?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried release version of app, but my problem doesn’t resolved

>Solution :

  1. 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;
    }
}
  1. If you have several child controls on that panel you might want to call SuspendLayout() and ResumeLayout().

  2. Use Refresh() instead of Invalidate() for an immediate update of your control.

  3. 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading