ListView in VirtualMode

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

The SelectedIndices appears to be broken. When the user shift selects a
range of items, the SelectedIndices property returns an empty collection.

If the SelectedIndices property is not available in virtual mode, how are we
to determine what has been selected?

CJL
 
Hi CJL,

I performed a test based on your description but I didn't reproduce the
problem.

I set up a Windows application project and drag&drop a ListView from
Toolbox onto the form. I set the VirtualMode property of the listview to
true and the VirtualListSize property to 10 and handle the
RetrieveVirtualItem event of the listview. I add a button on the form to
show the selected indices. The following is the code in the form.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.listView1.RetrieveVirtualItem += new
RetrieveVirtualItemEventHandler(listView1_RetrieveVirtualItem);
}

void listView1_RetrieveVirtualItem(object sender,
RetrieveVirtualItemEventArgs e)
{
ListViewItem item = new ListViewItem(e.ItemIndex.ToString());

e.Item = item;
}

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("SelectedIndices: " +
this.listView1.SelectedIndices.Count.ToString());
for (int i = 0; i < this.listView1.SelectedIndices.Count; i++)
{

MessageBox.Show(this.listView1.SelectedIndices.ToString());
}
}
}

Build the project and run it. If I select 3 items using Shift key in the
listview and click the button on the form, a message box pops up saying
'SelectedIndices: 3' firstly and three message boxes show up consequently
and each of them says the index of each selected item.

Is there any difference between your project and mine? If yes, you may show
me some sample code or send me your project that could just reproduce the
problem. To get my actual email address, remove the 'online' from my
displayed email address.

I look forward to your reply.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi,

Thank you for your reply.

I should clarify.

If you check the SelectedIndices property from within the
SelectedIndexChanged event after holding the shift key to select a range of
values, you will see that it returns an empty collection.

CJL

Linda Liu said:
Hi CJL,

I performed a test based on your description but I didn't reproduce the
problem.

I set up a Windows application project and drag&drop a ListView from
Toolbox onto the form. I set the VirtualMode property of the listview to
true and the VirtualListSize property to 10 and handle the
RetrieveVirtualItem event of the listview. I add a button on the form to
show the selected indices. The following is the code in the form.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.listView1.RetrieveVirtualItem += new
RetrieveVirtualItemEventHandler(listView1_RetrieveVirtualItem);
}

void listView1_RetrieveVirtualItem(object sender,
RetrieveVirtualItemEventArgs e)
{
ListViewItem item = new ListViewItem(e.ItemIndex.ToString());

e.Item = item;
}

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("SelectedIndices: " +
this.listView1.SelectedIndices.Count.ToString());
for (int i = 0; i < this.listView1.SelectedIndices.Count; i++)
{

MessageBox.Show(this.listView1.SelectedIndices.ToString());
}
}
}

Build the project and run it. If I select 3 items using Shift key in the
listview and click the button on the form, a message box pops up saying
'SelectedIndices: 3' firstly and three message boxes show up consequently
and each of them says the index of each selected item.

Is there any difference between your project and mine? If yes, you may show
me some sample code or send me your project that could just reproduce the
problem. To get my actual email address, remove the 'online' from my
displayed email address.

I look forward to your reply.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi CJL,

I performed another test. I handled the SelectedIndexChanged event of the
listview and checked the SelectedIndices property of the listview.

The result of my test is that when I hold the Shift key and select a range
of the list view items, the SelectedIndexChanged event is raised once and
the SelectedIndices property is empty.

If I don't hold the Shift key to select only one list view item at one
time, the SelectedIndexChanged event is raised twice. The event occuring
for the first time is caused by unselecting the previous selected item and
the SelectedIndices property is empty at this time. The event being raised
for the second time is caused by selecting the new item and the
SelectedIndices property has one element at this time.

It seems that the SelectedIndexChanged event isn't raised after the new
items are selected, when we hold the Shift key and selected a range of the
items.

I think the SelectedIndexChanged event makes sense only when one list view
item is selected at one time.

Hope this helps.
If you have any concerns, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
Hi,

Thank you for your reply, I am glad that you were able to reproduce the
problem.

The ListView control is, by default, in a multi-selection mode and we are
using it as such. The SelectedIndices property should correctly reflect the
change in selection when the event is raised, no matter how the change
occured.

The event title, SelectedIndexChanged, suggests a past tense. By this I
mean, when the event is raised, the selected indices should have already
changed and the value of the SelectedIndices property should correctly
reflect this change. By having a situation where, in some circumstances, the
SelectedIndices value is not correct when the control is notifying listeners
that there has been a change, is simply not the correct behavior. One would
expect a final SelectedIndexChanged event where the SelectedIndices property
was in sync with the actual selections made.

CJL
 
Hi CJL,

Thank you for your update.

Firstly, I would appologize to you that I made a mistake in my previous
reply.

The SelectedIndexChanged event occurs in single selection ListView
controls, whenever there is a change to the index position of the selected
item. In a multiple selection ListView control, this event occurs whenever
an item is removed or added to the list of selected items.

If the ListView control is not in virtual mode, the SelectedIndexChanged
event works, no matter we select a single item or select multiple items
using Ctrl or Shift key.

If the ListView is in virtual mode, the SelectedIndexChanged event works
when we select a single item or select multiple items using Ctrl key. If we
use Shift key to select a range of items, this event won't occur after the
selection finishes, but another event called
VirtualItemsSelectionRangeChanged will be raised. So we should handle this
event to get the correct value of the SelectedIndices property of the
ListView control.

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
Hi CJL,

How about the problem now?

If you have anything unclear, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!


Sincerely,
Linda Liu
Microsoft Online Community Support
 
Does anyone have a working code that shows how to get the list of
selected items when the ListView is in virtual mode?

I mean, please, we just want to get the list of items selected in a
list view!!! How hard should it be?!

Thanks.

Sincerely,
Damir Colak
Very Frustrated Developer
 
Back
Top