ListView problem in VB .NET
I have noticed many (sometimes non-deterministic) problems with the ListView
control in .NET (using from VB.NET Professional, release edition w/ .net
Framework sp1). Mostly, the issues I've seen seem to have to do with
imageLists, StateImageLists in LargeIcon display mode: image indexes
disappearing, layouts changing from execution to execution, images
disappearing, state images randomly appearing and disappearing.
Here is a deterministic bug. No code needed.
1. Create two image lists (ilLarge, ilStates) containing a couple of images
each. Use different images in each list.
2. Create ListView1 and assign ilLarge as the LargeImageList, ilStates as
the StateImageList.
3. Add an item to the ListView1.Items collection from the designer. Choose
an ImageIndex for the item (fine). Attempt to choose a StateImageIndex
(BUG: you will be presented with the LargeImageList, not the
StateImageList). Continue anyway, choosing StateItemIndex = 0. The
designer will correctly apply the 1st stateImage from ilStates.
4. You may save and reload this form now, and the state image appears fine.
now run the project. BUG: Viola, the state image is gone.
(Nondeterministic BUG): Sometimes it will be gone from the designer as
well - stateimageindex is back to (none), althought that didn't happen in
this simple case. It seems to have to do with in what container the list is
contained.
5. Even if you attempt to reset the stateImageList and StateImageindex from
code, it will (usually) fail.
6. While trying the simple case for replication, I saw an interested
run-time error message I hadn't seen before:
sender.StateImageList Run-time exception thrown :
System.MissingMemberException - Public member 'StateImageList' on type
'frmMain' not found. Perhaps the ListViewItems class is referencing one
container too far to find its StateImageList.
Eric Anderson
Quote:
> I believe I've found a ListView bug in Microsoft Visual Basic .NET
> Version 7.0.9466. If this is a bug what is the process for
> submitting it to Microsoft, how do I track it, and how will I know
> if / when it gets fixed? Thanks.
> The problem involves the detail view and initialization of the
> list view's large image list. If you initialize the large image
> list before adding ListViewItem objects it will never show any
> contents in the detail view.
> Place the following test case in Form1_Load of a Windows app
> that has a ListView control named ListView1:
> ****************************************************************
> ' These ImageList objects would contain images in real code.
> ' ImageList contents do not affect this problem. Static
> ' ImageList objects have the same behavior.
> '
> Dim imageListSmall As New ImageList()
> Dim imageListLarge As New ImageList()
> ' Associating the LargeImageList before adding ListViewItem objects
> ' causes the ListView to be empty when using the Detail view. List
> ' and Icon views work just fine.
> '
> 'ListView1.LargeImageList = imageListLarge
> ListView1.SmallImageList = imageListSmall
> ListView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left)
> ListView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left)
> ListView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left)
> ' First Load
> ListView1.BeginUpdate()
> ListView1.View = View.Details
> Dim item1 As New ListViewItem("item1")
> item1.SubItems.Add("1")
> item1.SubItems.Add("2")
> ListView1.Items.Add(item1)
> Dim item2 As New ListViewItem("item2")
> item2.SubItems.Add("3")
> item2.SubItems.Add("4")
> ListView1.Items.Add(item2)
> ListView1.LargeImageList = imageListLarge ' Must be called once.
> ListView1.EndUpdate()
> ListView1.View = View.SmallIcon ' User selected other views ...
> ListView1.View = View.List
> ListView1.Items.Clear() ' User action requires new ListView contents ...
> ' Second Load
> ListView1.BeginUpdate()
> ListView1.View = View.Details
> Dim item3 As New ListViewItem("item3")
> item3.SubItems.Add("a")
> item3.SubItems.Add("b")
> ListView1.Items.Add(item3)
> Dim item4 As New ListViewItem("item4")
> item4.SubItems.Add("c")
> item4.SubItems.Add("d")
> ListView1.Items.Add(item4)
> ListView1.EndUpdate()
> --
> Brian Wolfe, Senior Software Engineer / Architect
> o __________ Austin Provider Solutions, http://austinps.com
> _\__`[__________|
> ] [ \, ][ ][