.NET: ListView blues?

The selection counter, .SelectedItems.Count , does not change when the Selected property is true for an item in the Items collection ListView .

Example:

  lvPept.SelectedItems.Clear() lvPept.Items(6).Selected = True Dim newLen As Integer = lvPept.SelectedItems.Count 

lvPept is a ListView and contains 10 items.

newLen is expected to be 1, but equal to 0 when a problem occurs and the SelectedIndexChanged event is not . With other datasets equal 1, as expected, and the selected event SelectedIndexChanged.

Under what circumstances or in what condition can lvPept be in for this? BeginUpdate () / EndUpdate () is not used with lvPept .

Background:

I am trying to find a problem with one of the users of my open source .NET application, MSQuant ( http://msquant.sourceforge.net/ ).

I have run out of ideas about what might be causing this problem.

The problem is reproducible, and I can reproduce it in my Visual Studio 2008 development environment. It seems that regardless of the version of Windows (Windows 2000 / Windows XP 32 bit / Windows XP 64 bit), the version of .NET execution (2.0 / 3.5) and the Visual Studio version (2005/2008).

Another context: the application is written in VB.NET and in C # and is a Windows Forms application. The source code for the class is at http://shrinkster.com/14bg . the form class in which the ListView is located was originally generated by one of the earliest official versions of Visual Studio, which is supported by .NET, approx. 2002.

Update 1: since I have both a working case and a damaged case I compared the contents of lvPept . The only difference besides the “Handle”, “MousePosition” and “TopItem” properties (since this is a different protein with different peptides) is the “Created” property. This was false for a broken case. It makes sense that a partially constructed object cannot function properly, but how can this happen?

Update 2: The Created property, which is false, turned out to be good lead. I think the real problem was the construction time , not the form load time . I have now added ASSERT for the "Created" property, reorganized and changed all the operations should be performed at the time the form loads. Now it works as expected, and the user with the problem received a new version of the application.

The old bad way has been there since the app in 2002. I'm just wondering if some experts can throw light on why it worked 99.9% of the time and just failed in several cases and reproducibly.

+4
source share
2 answers

This is interesting.

The only thing I think could do this is if the ListView did not understand that the ListItem had changed its "Selected" value and therefore would not update the selected item collections.

The .SelectedItems property is not necessarily generated every time you get the property - if you mirror the System.Windows.Forms assembly:

  if (this.selectedListViewItemCollection == null) { this.selectedListViewItemCollection = new SelectedListViewItemCollection(this); } return this.selectedListViewItemCollection; 

So, I tend to think that you are getting an obsolete selectedListViewItemCollection.

I would try instead of changing the Selected property at the Item level, try adding the selected index to the .SelectedIndices ListView property instead and see if this works. Therefore, the ListView does not rely on the selection of ListViewItem.

+1
source

I believe that you also need to call ListView.Select () to fire the SelectedIndexChanged event.

 lvPept.SelectedItems.Clear() lvPept.Items(6).Selected = True lvPept.Select() Dim newLen As Integer = lvPept.SelectedItems.Count 
0
source

All Articles