ListView exception - stumped! [modified]
-
Hi all, I have a ListView control on a VS2003 WinForm which I am populating successfully with JPEG images using the LargeIcon setting. This works fine so I added code to handle the SelectedIndexChanged event so when the user clicks on one the icons (ie: the images displayed as thumbnails) the full image will appear in another tab. (The ListView MultiSelect property is turned off BTW) Ok, so this works perfectly the FIRST time... but when I try to select any image after that I get this error (marked in the code below where it occurs): An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in system.windows.forms.dll My event handler is below - anyone know what the heck is going wrong here?!!? My listview still seems to have all it's items and the correct indexes as well so I'm simply stumped as to what's going on. Mike PS: I'll google as well but I have a big deadline so any help will be appreciated! Private Sub lvwImages_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwImages.SelectedIndexChanged Dim ndx As Integer = lvwImages.SelectedItems(0).Index 'CODE FALLS OVER HERE!!!! Dim strImagePath As String = lvwImages.Items(ndx).SubItems(0).Text 'The image filename Dim img As Image = Image.FromFile(strImagePath) 'picFullImage is a Picturebox control on another tab. picFullImage.Width = img.Width picFullImage.Height = img.Height picFullImage.Image = img End Sub -- modified at 5:31 Friday 21st July, 2006
-
Hi all, I have a ListView control on a VS2003 WinForm which I am populating successfully with JPEG images using the LargeIcon setting. This works fine so I added code to handle the SelectedIndexChanged event so when the user clicks on one the icons (ie: the images displayed as thumbnails) the full image will appear in another tab. (The ListView MultiSelect property is turned off BTW) Ok, so this works perfectly the FIRST time... but when I try to select any image after that I get this error (marked in the code below where it occurs): An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in system.windows.forms.dll My event handler is below - anyone know what the heck is going wrong here?!!? My listview still seems to have all it's items and the correct indexes as well so I'm simply stumped as to what's going on. Mike PS: I'll google as well but I have a big deadline so any help will be appreciated! Private Sub lvwImages_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwImages.SelectedIndexChanged Dim ndx As Integer = lvwImages.SelectedItems(0).Index 'CODE FALLS OVER HERE!!!! Dim strImagePath As String = lvwImages.Items(ndx).SubItems(0).Text 'The image filename Dim img As Image = Image.FromFile(strImagePath) 'picFullImage is a Picturebox control on another tab. picFullImage.Width = img.Width picFullImage.Height = img.Height picFullImage.Image = img End Sub -- modified at 5:31 Friday 21st July, 2006
Try using the FocusedItem property of the ListView. from memory, try something like this you have
nzmike wrote:
Dim ndx As Integer = lvwImages.SelectedItems(0).Index 'CODE FALLS OVER HERE!!!! Dim strImagePath As String = lvwImages.Items(ndx).SubItems(0).Text 'The image filename
I suggest
...
Dim strImagePath As String = lvwImages.FocusedItem.SubItems(0).Text
...
Saves you having to declare the "ndx" variable as well. I find the FocusedItem property far more useful than SelectedIndex when using a ListView. I hope this solves your problem.
-
Try using the FocusedItem property of the ListView. from memory, try something like this you have
nzmike wrote:
Dim ndx As Integer = lvwImages.SelectedItems(0).Index 'CODE FALLS OVER HERE!!!! Dim strImagePath As String = lvwImages.Items(ndx).SubItems(0).Text 'The image filename
I suggest
...
Dim strImagePath As String = lvwImages.FocusedItem.SubItems(0).Text
...
Saves you having to declare the "ndx" variable as well. I find the FocusedItem property far more useful than SelectedIndex when using a ListView. I hope this solves your problem.
Thanks - hadn't used that before so it's good to know about. What I ended up doing (with my client's agreement) was making the user double-click on an image to show the image and that does work perfectly every time with the same code - I can only guess that the event handler for SelectedIndexChanged has a bug. Appreciate the input though - will use FocusedItem next time! -- modified at 5:59 Friday 21st July, 2006
-
Thanks - hadn't used that before so it's good to know about. What I ended up doing (with my client's agreement) was making the user double-click on an image to show the image and that does work perfectly every time with the same code - I can only guess that the event handler for SelectedIndexChanged has a bug. Appreciate the input though - will use FocusedItem next time! -- modified at 5:59 Friday 21st July, 2006
It's not a bug but I don't have a valid technical explanation for it. I think (i stand under correction) that it has to do with the variable's scope and how it's pointed to by the CLR. Never bothered to look it up, but I know it's not a bug.
-
Hi all, I have a ListView control on a VS2003 WinForm which I am populating successfully with JPEG images using the LargeIcon setting. This works fine so I added code to handle the SelectedIndexChanged event so when the user clicks on one the icons (ie: the images displayed as thumbnails) the full image will appear in another tab. (The ListView MultiSelect property is turned off BTW) Ok, so this works perfectly the FIRST time... but when I try to select any image after that I get this error (marked in the code below where it occurs): An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in system.windows.forms.dll My event handler is below - anyone know what the heck is going wrong here?!!? My listview still seems to have all it's items and the correct indexes as well so I'm simply stumped as to what's going on. Mike PS: I'll google as well but I have a big deadline so any help will be appreciated! Private Sub lvwImages_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwImages.SelectedIndexChanged Dim ndx As Integer = lvwImages.SelectedItems(0).Index 'CODE FALLS OVER HERE!!!! Dim strImagePath As String = lvwImages.Items(ndx).SubItems(0).Text 'The image filename Dim img As Image = Image.FromFile(strImagePath) 'picFullImage is a Picturebox control on another tab. picFullImage.Width = img.Width picFullImage.Height = img.Height picFullImage.Image = img End Sub -- modified at 5:31 Friday 21st July, 2006
Its not a bug. What is actually happening is the SelectedIndexChanged event is thrown twice when a new selection is make. The Selected Index is removed from the current selection and moved to a different selection. You could and should always check to make sure the SelectedItems Collection of the ListView has an item in it. Mike Lasseter
-
Its not a bug. What is actually happening is the SelectedIndexChanged event is thrown twice when a new selection is make. The Selected Index is removed from the current selection and moved to a different selection. You could and should always check to make sure the SelectedItems Collection of the ListView has an item in it. Mike Lasseter
Ok, that makes sense - I guess I didn't really think that it would get fired twice but it stands to reason I suppose since the first image is being de-selected that it would fire. I'm keeping with the double-click method now though as my client now prefers that over the single click anyway. Cheers, Mike