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,
![]()




Reply With Quote