0
private void downloader_Succeeded(object sender, EventArgs e)
        {
            FileDownloader.FileInfo fi = downloader.CurrentFile;
            string name = fi.Path;
            foreach (ListViewItem lvw in listView1.Items)
            {
                if (lvw.Text == name)
                    lvw.ForeColor = Color.Green;
            }
            label6.Text = countFilesDownloaded++.ToString();
        }

Each time when it's coloring the item it's flickering. I saw some answers but none of them is working.

I saw this answer:

Flickering answer

And this one

Flickering answer

Maybe i didn't use them right. But they didn't work.

Tried to add this to the form1 at the bottom:

protected override CreateParams CreateParams {
  get {
    var parms = base.CreateParams;
    parms.Style &= ~0x02000000;  // Turn off WS_CLIPCHILDREN
    return parms;
  }
}

Then tried to create instance of this each time a item was coloring inside the event.

Community
  • 1
  • 1
moshe ralf
  • 489
  • 3
  • 12
  • 21
  • 3
    The method desribed in the end is called automatically by Win forms (Windows) via recursion, You dont have to call that. As you just change color of 1 item, what is flickering? Whole listbox or the item? I would consider calling 'SuspendLayout()' on listbox at the start and its resume + 'Refresh()' at the end of download method – Tatranskymedved Feb 12 '17 at 01:17
  • 1
    Wouldn't this problem be situational where the majority of downloads are completed rather quickly? Is this a realistic situation? –  Feb 12 '17 at 01:40

1 Answers1

4

I had your problem and after doing some research it appears that the ListView control repaints its entire area whenever you modify a single item. The solution is to subclass the ListView and filter out the WM_ERASEBKGND message. This did the trick for me.

For more information see here: c# flickering Listview on update

public partial class ListViewNF : ListView
{
    public ListViewNF()
    {
    }

    public ListViewNF(IContainer container)
    {
        container.Add(this);

        InitializeComponent();

        //Activate double buffering
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer |  
        ControlStyles.AllPaintingInWmPaint, true);

        //Enable the OnNotifyMessage event so we get a chance to filter out 
        // Windows messages before they get to the form's WndProc
        this.SetStyle(ControlStyles.EnableNotifyMessage, true);
    }

    protected override void OnNotifyMessage(Message m)
    {
        //Filter out the WM_ERASEBKGND message
        if (m.Msg != 0x14)
        {
            base.OnNotifyMessage(m);
        }
    }
}
Community
  • 1
  • 1
Dave S
  • 973
  • 9
  • 17
  • What happens if you resize the window? Sometimes controls end up painting over themselves –  Feb 12 '17 at 10:05
  • @MickyD - No repainting or flicking occurs on resize. My ListView class uses multple colors to color lines as well as colors for the background of certain list items. – Dave S Feb 12 '17 at 10:46