Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code using ListView virtual mode still lack some of important information. Firstly, we need set value to <strong>VirtualListSize</strong> property of ListView. Secondly, we need setup a cache value to retrieve ListItem correctly.</p> <p>We should keep in mind the RetrieveVirtualItem event <strong>always required return a ListViewItem</strong>.</p> <p>You can reference at here: <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.virtualmode%28v=vs.90%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.virtualmode%28v=vs.90%29.aspx</a></p> <p>I have modified your code base on my assume, you can modify it following your idea. Hope this help.</p> <pre><code>public partial class Form1 : Form { private ListViewItem[] myCache; //array to cache items for the virtual list private int firstItem; //stores the index of the first item in the cache public Form1() { InitializeComponent(); _fileInfoCollection = new Queue&lt;ListViewFileInfo&gt;(); } private void GetFileInformation(string drive) { _fileInfoCollection.Clear(); var directory = new DirectoryInfo(drive); var files = directory.GetFiles("*.*", SearchOption.TopDirectoryOnly); myCache = new ListViewItem[files.Length]; int temp = 0; foreach (var file in files) { _fileInfoCollection.Enqueue(new ListViewFileInfo() { FileName = file.Name, FilePath = file.FullName }); ListViewFileInfo fileInfo = _fileInfoCollection.Dequeue(); var listViewItem = new ListViewItem(); listViewItem.Text = fileInfo.FileName; var listViewSubItem = new ListViewItem.ListViewSubItem(); listViewSubItem.Text = fileInfo.FilePath; listViewItem.SubItems.Add(listViewSubItem); myCache[temp] = listViewItem; temp++; } listView1.VirtualListSize = myCache.Length; } private void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e) { if (myCache != null &amp;&amp; e.ItemIndex &gt;= firstItem &amp;&amp; e.ItemIndex &lt; firstItem + myCache.Length) { //A cache hit, so get the ListViewItem from the cache instead of making a new one. e.Item = myCache[e.ItemIndex - firstItem]; } else { //A cache miss, so create a new ListViewItem and pass it back. e.Item = new ListViewItem(); } } private void comboBoxDrive_SelectedIndexChanged(object sender, EventArgs e) { GetFileInformation(comboBoxDrive.Text); } private Queue&lt;ListViewFileInfo&gt; _fileInfoCollection; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload