I’m using a ListView control in the details view with VirtualMode set to true and I’m finding that whenever a new item is added the horozontal scrollbar is set to the leftmost position. This doesn’t happen if the ListView is not in virtual mode. Update: this also doesn’t happen if you don’t enable visual styles.
You can see this by creating a simple ListView with 2 columns in details mode and adding something like the following:
Timer timer = new Timer();
public Form1()
{
this.InitializeComponent();
this.listView1.VirtualMode = true;
this.listView1.RetrieveVirtualItem += new RetrieveVirtualItemEventHandler(listView1_RetrieveVirtualItem);
this.listView1.VirtualListSize = 10;
timer.Interval = 250;
timer.Tick += new EventHandler(t_Tick);
timer.Start();
}
void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
e.Item = new ListViewItem(new string[] {"Test", ""});
}
void t_Tick(object sender, EventArgs e)
{
this.listView1.VirtualListSize += 1;
}
Scroll the list view to the right to see this effect.
How can I stop this from happening? I have an application where items are continuously added to the list view and so this behaviour is very distracting.
This appears to be a known bug: ListView in virtual mode scrolls incorrectly
The last comment from Microsoft on that Connect report is:
Of course that was back in 2005, so maybe it’s fixed in Win 8?
In the meantime, the workaround appears to be:
Create new class that inherits ListView and write the following code:
Now update your code to use SetVirtualListSize method instead of the original VirtualListSize property.
reference:
http://msdn.microsoft.com/en-us/library/bb761188%28VS.85%29.aspx