Hello,

I want to search a specific column in ListView. I've coded searching however it always defaults to search the first column, how can i specify which column to search?

Here is my code:

Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
        If txtSearchThoughts.Text IsNot "" Then
            'ListViewSearch.Focus()

            'For i = 0 To ListViewSearch.Items.Count - 1
            'If ListViewSearch.Items(i).SubItems(0).Text = txtSearchThoughts.Text Then
            'ListViewSearch.Items(i).Selected = True
            'End If
            'Next
            Dim itm As ListViewItem
            Dim i As Integer

            For i = 0 To ListViewSearch.Items.Count - 1
                ListViewSearch.Items(i).Selected = False
                ListViewSearch.Items(i).BackColor = Color.White
            Next

            With ListViewSearch
                itm = .FindItemWithText(txtSearchThoughts.Text, False, 0, True)

                If Not itm Is Nothing Then

                    '.TopItem = itm
                    .Items.Item(itm.Index).BackColor = Color.BurlyWood
                    .Items.Item(itm.Index).EnsureVisible()
                Else
                    MsgBox("No Record Found!")
                    For i = 0 To ListViewSearch.Items.Count - 1
                        ListViewSearch.Items(i).Selected = False
                        ListViewSearch.Items(i).BackColor = Color.White
                    Next
                    .Items(0).EnsureVisible()
                    .Items.Item(0).BackColor = Color.BurlyWood

                    txtSearchThoughts.SelectionStart = 0
                    txtSearchThoughts.SelectionLength = Len(txtSearchThoughts.Text)
                    txtSearchThoughts.Focus()
                End If
            End With
            itm = Nothing

        End If
    End Sub

Recommended Answers

All 2 Replies

The following works however i cannot figure out how to add error handling:

        If txtSearchThoughts.Text IsNot "" Then
            ListViewSearch.Focus()

            For i = 0 To ListViewSearch.Items.Count - 1
                'Searches second column: SubItems(2)
                If ListViewSearch.Items(i).SubItems(2).Text = txtSearchThoughts.Text Then
                    ListViewSearch.Items(i).Selected = True
                End If
            Next

        End If
commented: Nice, you changed to subitems(2) +12
If txtSearchThoughts.Text IsNot "" Then

''Change to ....

If Not txtSearchThoughts.Text = "" Then

Also, good conventional coding is to use vbNullString and not "" ... :)

If Not txtSearchThoughts.Text = vbNullString Then

To add error trapping, in the sub where you want to trap...

On error GoTo MyErrTrap

''All your code here as normal....

''IMPORTANT!!! After the code add exit sub...

Exit Sub

MyErrorTrap:
''Code here to do if an error occured...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.