-1

I have designed a dashboard like winform and when I try to resize the winform it flickers too much.

I have already tried SuspendLayout and enabled DoubleBufferring but still, the issue persists. Please check the following GIF.

WinForm flickring While Resizing GIF

EDIT

Here is the code for Gradient Panel:

 this.bunifuGradientPanel1.BackColor = System.Drawing.Color.Transparent;
            this.bunifuGradientPanel1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("bunifuGradientPanel1.BackgroundImage")));
            this.bunifuGradientPanel1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.bunifuGradientPanel1.Controls.Add(this.panel1);
            this.bunifuGradientPanel1.Controls.Add(this.panel4);
            this.bunifuGradientPanel1.Controls.Add(this.panel3);
            this.bunifuGradientPanel1.Controls.Add(this.panel5);
            this.bunifuGradientPanel1.Controls.Add(this.panel6);
            this.bunifuGradientPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.bunifuGradientPanel1.GradientBottomLeft = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
            this.bunifuGradientPanel1.GradientBottomRight = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(192)))));
            this.bunifuGradientPanel1.GradientTopLeft = System.Drawing.Color.Purple;
            this.bunifuGradientPanel1.GradientTopRight = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(128)))));
            this.bunifuGradientPanel1.Location = new System.Drawing.Point(0, 0);
            this.bunifuGradientPanel1.Name = "bunifuGradientPanel1";
            this.bunifuGradientPanel1.Quality = 10;
            this.bunifuGradientPanel1.Size = new System.Drawing.Size(1020, 680);
            this.bunifuGradientPanel1.TabIndex = 0;

Thank you for your help in advance.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
Aimal
  • 97
  • 1
  • 9
  • I have also tried previous solutions provided in the forum but still no luck. – Aimal Feb 06 '21 at 06:05
  • Go old-school. During resizing, manage the invalidation region (if you are shrinking, you don't need to invalidate at all, when expanding you really only need to invalidate the one or two rectangles that are being uncovered). During your resizing operation, don't do a full gradient. Instead, capture a bitmap of the rectangle next to what is being uncovered, and use it for redrawing; once the resize is finished, re-invalidate all of the uncovered region. The last time I did something like this, it was with MFC, back in the 1990s. I'm sure it's possible with WinForms – Flydog57 Feb 06 '21 at 06:14
  • 1
    you should probably show the code that does the painting. Or create a minimal reproducible example. Saying "It doesn't work" and not showing your work helps nobody. – Andy Feb 06 '21 at 06:40
  • @Andy Thank you, I have edited the question and added the code. – Aimal Feb 06 '21 at 06:57
  • @Flydog57 Can you please elaborate on this? I am already using the gradient as bitmap. Thank you. – Aimal Feb 06 '21 at 06:57
  • 1
    _I have also tried previous solutions provided in the forum_ Which and how and how didn't they work??? - The standard solution is to __double-buffer__ the control you paint on. Or pick a control that is double-buffered out of the box, like Picturebox or Label. Panels are __not__ really meant to draw on! - Also: You don't show any code exept the creation. Is there no other relevant code like Paint or Resize code? – TaW Feb 06 '21 at 08:57
  • @TaW No there is no Paint or Resize code, I have designed this only using the GUI and a ThirdParty Framework (Bunifu). – Aimal Feb 06 '21 at 09:23
  • 1
    Well if all the drawing is done in the ThirdParty Framework (Bunifu) then you will have to look for a solution there. Step one: Add a correct tag..! - Resizing a control with an backgroundimage will never flicker by itself; but I imagine that the control needs to redraw its gradient when the size changes.. Maybe, just maybe you can [inject](https://stackoverflow.com/questions/44185298/update-datagridview-very-frequently/44188565#44188565) double-buffering – TaW Feb 06 '21 at 09:59
  • Thanks for the help, I managed to solve it on my own. I have posted the answer. – Aimal Feb 06 '21 at 12:27

1 Answers1

1

Finally solved this issue!

Here is the correct answer just in case anyone may face this problem in the future:

First, create the following function inside your Form.cs:

//Double Buffering Function
        public static void SetDoubleBuffering(System.Windows.Forms.Control control, bool value)
        {
            System.Reflection.PropertyInfo controlProperty = typeof(System.Windows.Forms.Control)
                .GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            controlProperty.SetValue(control, value, null);
        }

Then simply call the function in your Form1_Load function and pass the name of the control (panel in my case 'bunifuGradientPanel1') and you're ready to go:

 private void Form1_Load(object sender, EventArgs e)
        {
            // Enabling Double Buffering for BunifuGradientPanel
            SetDoubleBuffering(bunifuGradientPanel1, true);
        }

Hope this helps anyone facing the flickering issue while working with Panels in Windows Forms c#.

Thanks to Fluxbytes.com for creating the function above and posting it on their forum.

Aimal
  • 97
  • 1
  • 9