Question Work with Listview Events?

Knvn

Well-known member
Joined
Dec 30, 2009
Messages
45
Programming Experience
Beginner
Hi,
I have 2 event handlers (i.e. ListView1_SelectedIndexChanged and ListView_DoubleClick). When I double click on a perticuler item in listview both the events will be raised and controll will first passes to ‘ListView1_SelectedIndexChanged handler’ how can I rid from this ?
 
If the user double clicks on the already selected item then the index doesn't change and the SelectedIndexChanged event shouldn't fire.
 
VB.NET:
Expand Collapse Copy
Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

        If Not isRecursive Then
            If Not PreviouslySelected = -1 Then
                ListView1.Items.Item(PreviouslySelected).BackColor = Color.White
                ListView1.Items.Item(PreviouslySelected).ForeColor = Color.Black
            End If

            If CheckBox1.Checked = True Then
                ListView1.SelectedItems(0).BackColor = Color.DodgerBlue
                ListView1.SelectedItems(0).ForeColor = Color.White
                PreviouslySelected = ListView1.SelectedItems(0).Index
                Dim ts = Mid(ListView1.SelectedItems(0).ToString, 16, Len(ListView1.SelectedItems(0).ToString))
                ts = ts.Remove(ts.Length - 1)
                Try
                    PictureBox1.Image = Image.FromFile(globaldata & "\Photos\" & ts & ".jpg")
                Catch ex As Exception
                    PictureBox1.Image = Global.WindowsApplication1.My.Resources.Resources.rbphoto
                End Try
                Me.isRecursive = True
                ListView1.SelectedIndices.Clear()
            End If
        Else
            Me.isRecursive = False
        End If


    End Sub

Private Sub ListView_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick

        Dim ts = Mid(ListView1.SelectedItems(0).ToString, 16, Len(ListView1.SelectedItems(0).ToString))
        ts = ts.Remove(ts.Length - 1)

        searchForm.receivedDetail = ts
        searchForm.Show()

        isFrmMainNedded = False
        Me.Close()

    End Sub

In the ‘ListView1_SelectedIndexChanged’ I cleared the ‘SelectedIndices’ Because it generates to following error:

InvalidArgument=Value of '0' is not valid for 'index'. Parameter name: index

In the line,

Dim ts = Mid(ListView1.SelectedItems(0).ToString, 16, Len(ListView1.SelectedItems(0).ToString))

When user double clicks on the particular item, the same error is generated in ‘ListView_DoubleClick’ but If I comment out the ‘ListView1_SelectedIndexChanged’ then it works fine.
 
Back
Top