Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. ListView exception - stumped! [modified]

ListView exception - stumped! [modified]

Scheduled Pinned Locked Moved Visual Basic
helpdatabasequestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nzmike
    wrote on last edited by
    #1

    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

    D M 2 Replies Last reply
    0
    • N nzmike

      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

      D Offline
      D Offline
      Dave Sexton
      wrote on last edited by
      #2

      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.

      N 1 Reply Last reply
      0
      • D Dave Sexton

        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.

        N Offline
        N Offline
        nzmike
        wrote on last edited by
        #3

        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

        D 1 Reply Last reply
        0
        • N nzmike

          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

          D Offline
          D Offline
          Dave Sexton
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • N nzmike

            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

            M Offline
            M Offline
            mr_lasseter
            wrote on last edited by
            #5

            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

            N 1 Reply Last reply
            0
            • M mr_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

              N Offline
              N Offline
              nzmike
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups