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

Are you facing 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 avoid 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.

Now when Windows forms or control flicker? Following are few scenarios where your windows forms application flickers –

  1. On loading of a form or control.
  2. On resizing of a form or control.

Why do Windows forms or control flickers?

A simple answer to this is – possibly multiple controls are present either on your form or on your control itself. That impacts your application loading as well as if you are generating the application view dynamically with more controls.

Avoid Flickering in C#.Net
Flickering issues in .Net

How to avoid flickering in c#?

In this article we are going to see how to avoid flickering in C#.net. But first understand that the issue can be solved by 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 Second way is the best.

  1. Enable double buffering in C#

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

    this.DoubleBuffered = true;

    For example –

    public a1ashiishcsharptutorialsMainForm()
    this.DoubleBuffered = true;

    Now see below code –

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

    Above code snippet is equivalent to –
    this.DoubleBuffered = 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 control level. Considering a form as your control, it would reduce the flickering of form only.

    Hence you would need to apply doublebuffering 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;
    publicpartial class a1ashiishcsharptutorialsTLPanel : TableLayoutPanel
    public a1ashiishcsharptutorialsTLPanel()
    this.DoubleBuffered = true; //  SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);
    publica1ashiishcsharptutorialsTLPanel(IContainer container)
    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 only second method can help you to reduce the flickering.

  2. Handle CreateParams

    The problem of DoubleBuffering is that it get applies only on the Control level. To get rid of this issue, we have other 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 paste following code in form level. Whenever the handle calls CreateParams, a new style would get applied to your form and control to avoid flickering.

    protectedoverride CreateParams CreateParams
    CreateParamshandleParam = base.CreateParams;
    handleParam.ExStyle |= 0x02000000;   // 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 createparams C# is best way to handle flickering in C#.

Apply above methods to get flicker free control in c#.

In this c# tutorial we learned how to avoid flickering in c#. If you find the information is worth, then please share the article on your social media because sharing is caring.

More articles –

About the Author: Ashish Ramteke

Ashish Ramteke is a Software Professional, Part time blogger and founder of C# Tutorials, a popular blog on C#.Net. Ashish is having 6 years of work experience and an engineering degree in Computer Science and Engg.

2 Comments+ Add Comment

  • Thanks a lot man.

  • Thanks

Leave a comment