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

Time:11-11

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

CodePudding user response:

  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.

  • Related