2

I've been pulling my hair out over this C#/.NET problem where a ListView in virtual mode will not return a list of selected indices when more than one item is selected. When one or zero items are selected, everything works as expected. I've created the (minimal-ish?) code below to illustrate my problem. I'm sure I'm overlooking something really obvious, but I come from a C/C++ background and have very little experience with .Net, especially GUI code.

using System;
using System.Windows.Forms;

namespace AnotherListViewTest
{
    public partial class Form1 : Form
    {
        private System.Windows.Forms.ListView listView;
        private System.Windows.Forms.ColumnHeader columnHeader1;

        public Form1()
        {
            //InitializeComponent();
            this.listView = new System.Windows.Forms.ListView();
            this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.SuspendLayout();
            // 
            // listView
            // 
            this.listView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.listView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader1});
            this.listView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
            this.listView.Location = new System.Drawing.Point(8, 8);
            this.listView.Name = "listView";
            this.listView.Size = new System.Drawing.Size(676, 451);
            this.listView.TabIndex = 6;
            this.listView.UseCompatibleStateImageBehavior = false;
            this.listView.View = System.Windows.Forms.View.Details;
            this.listView.RetrieveVirtualItem += new System.Windows.Forms.RetrieveVirtualItemEventHandler(this.listView_RetrieveVirtualItem);
            this.listView.SelectedIndexChanged += new System.EventHandler(this.listView_SelectedIndexChanged);
            // 
            // columnHeader1
            // 
            this.columnHeader1.Width = 600;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(696, 471);
            this.Controls.Add(this.listView);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listView.VirtualMode = true;
            listView.VirtualListSize = 1000;
        }

        private void listView_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
        {
            e.Item = new ListViewItem(new String[] { e.ItemIndex.ToString() });
        }

        private void listView_SelectedIndexChanged(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Number of selected items: " + listView.SelectedIndices.Count);
        }
    }
}
Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
nohupper
  • 55
  • 5
  • 4
    Possible duplicate of [How to use Virtual mode in ListView?](https://stackoverflow.com/questions/5415877/how-to-use-virtual-mode-in-listview) – Rufus L Feb 05 '18 at 22:27
  • Do you know what VirtualMode is and do you really need it in that mode? – CodingYoshi Feb 05 '18 at 22:37

1 Answers1

2

I needed to use the VirtualItemsSelectionRangeChanged event for multiple selections.

nohupper
  • 55
  • 5
  • Notable weirdness: I’m seeing this event only needed when multi-selecting with Shift. Not with Ctrl or mouse drag. Also, when multi-selecting with Shift, SelectedIndexChanged still fires with SelectedIndices.Count=0 prior to VirtualItemsSelectionRangeChanged firing with SelectedIndices.Count at the expected value. – Stephen Klancher Jul 18 '19 at 03:22