Results 1 to 5 of 5

Thread: Crashing when right clicking listview

  1. #1

    Thread Starter
    Addicted Member Porsche944's Avatar
    Join Date
    Apr 2005
    Location
    Ann Arbor
    Posts
    182

    Exclamation Crashing when right clicking listview

    I have some code in my MouseUp event for my list view that copies the currently selected index to the clip board. Everything works fine unless you right click on an area on the list view that is no a record. I get this error.


    "An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in system.windows.forms.dll

    Additional information: Specified argument was out of the range of valid values"

    it happens on this line
    VB Code:
    1. Dim lvItem As ListViewItem = lvResults.SelectedItems(0)

    VB Code:
    1. Private Sub lvResults_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvResults.MouseUp
    2.         'This function is triggered when the mouse button is pressed on the list view control
    3.         'If the mouse button clicked was the right mouse button then copy the
    4.         'description of the currently selected item.
    5.  
    6.         If e.Button = MouseButtons.Right Then
    7.             Dim lvItem As ListViewItem = lvResults.SelectedItems(0)
    8.             If lvItem.SubItems(1).Text = "" Then
    9.             Else
    10.                 Clipboard.SetDataObject(lvItem.SubItems(1).Text, True)
    11.                 UpdateStatus(lvItem.SubItems(1).Text & " copied to clipboard")
    12.             End If
    13.         End If
    14.     End Sub

    What error handling can I add to this routine so the program won't crash. I can't figure this out and I'm running out of idea's to try. I'm sure it's something simple that I am overlooking.

    Thank you,

  2. #2
    Member apolizoi's Avatar
    Join Date
    Jan 2005
    Location
    Greece
    Posts
    44

    Re: Crashing when right clicking listview

    include your code into a Try ...Catch .. Finally block to handle your errors..

  3. #3
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Crashing when right clicking listview

    Make sure that you have an item selected in your listview:


    VB Code:
    1. If  lvResults.SelectedItems.Count > 0 Then
    2.        If e.Button = MouseButtons.Right Then
    3.             Dim lvItem As ListViewItem = lvResults.SelectedItems(0)
    4.             If lvItem.SubItems(1).Text = "" Then
    5.             Else
    6.                 Clipboard.SetDataObject(lvItem.SubItems(1).Text, True)
    7.                 UpdateStatus(lvItem.SubItems(1).Text & " copied to clipboard")
    8.             End If
    9.         End If
    10.   end if

    But also listen to apolizoi about adding Try Catch blocks to your code.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Crashing when right clicking listview

    You should definitely use Try...Catch blocks in areas of your code where FACTORS BEYOND YOUR CONTROL may cause an exception to be thrown. These would include IO and data access operations, amongst others. This is not one of those times, though. As Negative0's code shows, there is no need for an exception to be thrown because you can check yourself beforehand whether your code will be valid or not. You should not just use exception handling willy-nilly as a substitute for writing efficient, robust code. Just the use of exception handlers has an impact, although it's very slight in most cases. Actually throwing an exception is an expensive operation though, and should be avoided if at all possible. Never use exception handling to control program flow if it is at all possible to avoid.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member Porsche944's Avatar
    Join Date
    Apr 2005
    Location
    Ann Arbor
    Posts
    182

    Re: Crashing when right clicking listview

    That's what I needed the selecteditems count. Why didn't I think of that before. :O

    Thanks for the help guys and your right this error dosen't randomly pop up and it should be handled correctly. I only use the Try Catch's on area's of code I haven't experianced an error on yet but, seem likely to produce one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width