2

I would like to update some or all of my listview's items and subitems contents with a timer (1 second refresh) But the listview flicker each one second. Sometimes the subitems are lost during redrawing. Because my listview contains data that is to be likely changed anytime, I use a timer.

Code: I put this function in the timer's Tick method

void Refresh()
{
   foreach(string s in lsttring)
   {
      lv.items.add(s);
      lv.items[i].subitems.add(i);
   }
}

I expect only items content (item text and subitem text) that are changed will be changed not the whole listview along with the timer tick.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
CPoundLerner
  • 51
  • 1
  • 2
  • there are a lot of questions with good answers regarding how to update list view from an other thread, try search please – sll Dec 04 '11 at 21:19

2 Answers2

5

The ListView control supports double buffering, it maps the DoubleBuffered property to the native control's LVS_EX_DOUBLEBUFFER style flag. It is quite effective but you can't get to it directly since it is a protected property. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form, replacing the old one.

using System;
using System.Windows.Forms;

class BufferedListView : ListView {
    public BufferedListView() {
        this.DoubleBuffered = true;
    }
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

Try this:

void Refresh()
{
   lv.BeginUpdate();
   foreach(string s in lsttring)
   {
      lv.items.add(s);
      lv.items[i].subitems.add(i);
   }
   lv.EndUpdate();
}

In this manner you update all the items and listview will be refreshed only at the end of this operation.
From Microsoft:

...if you want to add items one at a time using the Add method of the ListView.ListViewItemCollection class, you can use the BeginUpdate method to prevent the control from repainting the ListView every time that an item is added. When you have completed the task of adding items to the control, call the EndUpdate method to enable the ListView to repaint. This way of adding items can prevent flickered drawing of the ListView when lots of items are being added to the control.

Marco
  • 56,740
  • 14
  • 129
  • 152
  • Doesn't help, it still flickers on the EndUpdate call. – Hans Passant Dec 04 '11 at 21:33
  • @HansPassant: is this because refresh rate is too high? – Marco Dec 04 '11 at 21:39
  • It just gets noticeable with frequent updates, 1 second is very visible. Listview is a bit of a flicker bug since it tends to cram a lot of text in Details view. That's why it has the LVS_EX_DOUBLEBUFFER style flag. – Hans Passant Dec 04 '11 at 21:56
  • Thanks for your comment @HansPassant, I've learned something new!! I've just upvoted your solution, because it's simple and functional, great job! :) – Marco Dec 04 '11 at 22:03