visual c# form update results in flickering

前端 未结 10 1042
眼角桃花
眼角桃花 2020-12-17 21:18

I have a .net app that I\'ve written in c#. On some forms I frequent update the display fields. In some cases every field on the form (textboxes, labels, picturebox, etc) ha

相关标签:
10条回答
  • 2020-12-17 21:50

    You can just replace original control with custom one which has protected DoubleBuffered property to true. E.g. for ListView it would be something like this:

    internal class DoubleBufferedListView : ListView {
    
        public DoubleBufferedListView()
            : base() {
            this.DoubleBuffered = true;
        }
    
    }
    

    After that you just visit *.Designer.cs file and replace all mentions of native control with this one.

    P.S. Instead of inheriting from control you can also set this property via reflection:

    listView1.GetType().GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(lsvReport, true, null);
    

    It is not clean nor recommended but it requires no changes in *.Designer.cs files.

    0 讨论(0)
  • 2020-12-17 21:54

    You can double buffer almost every windows forms control, although most of the time it requires that you inherit from the desired control and override a protected property. Be cautioned, though, that I've spent quite a bit of time on the same issue and I've yet to fully remove flicker on my more complex forms.

    If you want truly flicker-free windows, I suggest looking at WPF.

    0 讨论(0)
  • 2020-12-17 21:55

    the short answer is

    SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
    

    the long answer is: see MSDN or google

    just for fun, try calling Application.DoEvents() after each element is updated, and see if the problem gets better or worse ;-)

    0 讨论(0)
  • 2020-12-17 22:00

    The ghosting is usually caused because you're running in a single thread and it's being held up with the field updates so the paint event doesnt fire. One way to fix this would be to put the heavy lifting in asynchronous methods. This will allow the form to repaint itself and update whatever is needed when they async method calls back.

    0 讨论(0)
提交回复
热议问题