C#.Net How To: Avoid Flickering in C#.net

C#.Net How To: Avoid Flickering in C#.net,flickering,windows forms application?,how to fix flickering in C#.net,Enable double buffering in C# ,Handle CreateParams
Share it:
Are you facing a flickering issue on your windows forms application? Are controls gets flickered on loading? Have you ever faced these issues? and don’t know how to stop the flickering? If yes, then you are at the right place to learn how to fix flickering in C#.net. And for your information my friend, you are not alone who is facing this problem. It’s not you who made mistake in development, in fact, this is a known issue of .Net (Windows forms) itself.

C#.Net How To: Avoid Flickering in C#.net

Now when Windows forms or control flicker? Following are a few reasons when windows forms flicker –

  1. On loading of a form of control.
  2. On resizing of a form of control.
Why do Windows forms or control flickers? A simple answer to this is – might be multiple controls are there either on your form or on your controls. That impacts your application loading as well as if you are generating the application view dynamically with more controls.

In this article, we are going to see how to avoid flickering in C#.net. But first, understand that the issue can be solved in 2 ways. The first way is to enable the double buffering in c# and the second way is to handle the CreateParams property. In most of the cases, double buffering works like a charm. However, if you applied double buffering and still the flickering issue persists then the Second way is the best.

(A) Enable double buffering in C# -


If only windows form is getting flickered, then adding a below line after InitializeComponent(); informs constructor would work for you.

this.DoubleBuffered = true;

For example –
public trickssMainForm()
{
InitializeComponent();
this.DoubleBuffered = true;
}


The below code is equivalent and you can use it instead of this.DoubleBuffered = true;

SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);



This would definitely reduce the flickering; However, if it doesn’t solve your problem completely and your controls are still flickering then you would need to add above code line in your custom controls constructor also. 

Why? Because DoubleBuffering works only on the control level.

Considering a form as your control, it would reduce the flickering of form only. Hence you would need to apply double-buffering to your control. 

Please note that you would need only custom control to avoid flickering. For example, if you have TableLayoutPanel in your form. 

Then you need to make it a custom control to avoid flickering in TableLayoutPanel. See below a class (.cs) file code of a custom control inherited from TableLayoutPanel.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public partial class tricksTLPanel : TableLayoutPanel
{
public tricksTLPanel()
{
InitializeComponent();
this.DoubleBuffered = true; //  SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);
}
public tricksTLPanel(IContainer container)
{
container.Add(this);
InitializeComponent();
this.DoubleBuffered = true; //  SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);
}
}

Note: Remove InitializeComponent(); in case not needed, or you get any errors. This would reduce the flickering issue in windows application. 
If it won’t work, then my friend's only second method can help you to reduce the flickering.

B) Handle CreateParams –

The problem of DoubleBuffering is that it get applies only to the Control level. To get rid of this issue, we have another way thru which if we can apply some style to Form then automatically it would get apply to all other controls on the form.
Copy and Put the following code in form level. Whenever the handle calls CreateParams, a new style would get applied to your form and control to avoid flickering.

protected override CreateParams CreateParams
{
get
{
CreateParams handleParam = base.CreateParams;
handleParam.ExStyle |= 0×02000000;   // WS_EX_COMPOSITED       
return handleParam;
}
}
Now, this approach (WS_EX_COMPOSITED) has also its own issues for ex. in XP, it doesn’t support window minimize/maximize animations. In this case, you need to be very tricky on when to use WS_EX_COMPOSITED and when to go with normal flow. In short, the create params C# is the best way to handle flickering in C#.

Apply the above methods to get flicker-free control in c#. 
If you find the information is worth it, then please share the article on your social media because sharing is caring. 

Do you know:-

If you like the tutorial, then please share this tutorial with your friends on social media.

Share it:

Windows Forms

Post A Comment:

0 comments: