|
-
Nov 14th, 2005, 04:21 PM
#1
Thread Starter
Addicted Member
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:
Dim lvItem As ListViewItem = lvResults.SelectedItems(0)
VB Code:
Private Sub lvResults_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvResults.MouseUp
'This function is triggered when the mouse button is pressed on the list view control
'If the mouse button clicked was the right mouse button then copy the
'description of the currently selected item.
If e.Button = MouseButtons.Right Then
Dim lvItem As ListViewItem = lvResults.SelectedItems(0)
If lvItem.SubItems(1).Text = "" Then
Else
Clipboard.SetDataObject(lvItem.SubItems(1).Text, True)
UpdateStatus(lvItem.SubItems(1).Text & " copied to clipboard")
End If
End If
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,
-
Nov 14th, 2005, 04:25 PM
#2
Member
Re: Crashing when right clicking listview
include your code into a Try ...Catch .. Finally block to handle your errors..
-
Nov 14th, 2005, 04:34 PM
#3
Re: Crashing when right clicking listview
Make sure that you have an item selected in your listview:
VB Code:
If lvResults.SelectedItems.Count > 0 Then
If e.Button = MouseButtons.Right Then
Dim lvItem As ListViewItem = lvResults.SelectedItems(0)
If lvItem.SubItems(1).Text = "" Then
Else
Clipboard.SetDataObject(lvItem.SubItems(1).Text, True)
UpdateStatus(lvItem.SubItems(1).Text & " copied to clipboard")
End If
End If
end if
But also listen to apolizoi about adding Try Catch blocks to your code.
-
Nov 14th, 2005, 05:03 PM
#4
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.
-
Nov 15th, 2005, 08:28 AM
#5
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|