Results 1 to 8 of 8

Thread: How to modify ListView search code to search in ListView Column 5, instead of Col. 0?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    274

    How to modify ListView search code to search in ListView Column 5, instead of Col. 0?

    How can I modify the following ListView search code to search in ListView Column 5, instead of Column 0?

    Code:
    Private Sub txtSearch_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
            Dim itm As ListViewItem
            Dim i As Integer
    
            For i = 0 To ListView1.Items.Count - 1
                ListView1.Items(i).Selected = False
                ListView1.Items(i).BackColor = Color.White
            Next
    
            With ListView1
                itm = .FindItemWithText(txtSearch.Text, True, 0, True)
    
                If Not itm Is Nothing Then
    
                    .Items.Item(itm.Index).BackColor = Color.Green
                    .Items.Item(itm.Index).EnsureVisible()
                Else
                    MsgBox("No Record Found!")
                    For i = 0 To ListView1.Items.Count - 1
                        ListView1.Items(i).Selected = False
                        ListView1.Items(i).BackColor = Color.White
                    Next
                    .Items(0).EnsureVisible()
                    .Items.Item(0).BackColor = Color.BurlyWood
    
                    txtSearch.SelectionStart = 0
                    txtSearch.SelectionLength = Len(txtSearch.Text)
                    txtSearch.Focus()
                End If
            End With
            itm = Nothing
        End Sub

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,677

    Re: How to modify ListView search code to search in ListView Column 5, instead of Col

    While I've never used a ListView(seriously, I think they're useless), I looked up the FindItemWithText method on MSDN. I found that you're passing a 0 in the startIndex parameter, what happens if you pass a 4?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    274

    Re: How to modify ListView search code to search in ListView Column 5, instead of Col

    It throws the following error:
    {"InvalidArgument=Value of '4' is not valid for 'startIndex'.
    Parameter name: startIndex"}

    So, what to use instead of ListView? Please advise.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,677

    Re: How to modify ListView search code to search in ListView Column 5, instead of Col

    It throws the following error:
    {"InvalidArgument=Value of '4' is not valid for 'startIndex'.
    Parameter name: startIndex"}
    Once again I read the MSDN documentation and found this:
    ArgumentOutOfRangeException
    startIndex is less than 0 or more than the number of items in the ListView.
    So, what to use instead of ListView? Please advise.
    If you're displaying data then I'd suggest that you use a DataGridView.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    274

    Re: How to modify ListView search code to search in ListView Column 5, instead of Col

    Thanks for your kind advice.

  6. #6
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: How to modify ListView search code to search in ListView Column 5, instead of Col

    Quote Originally Posted by VS2013 View Post
    How can I modify the following ListView search code to search in ListView Column 5, instead of Column 0?

    Code:
    Private Sub txtSearch_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
            Dim itm As ListViewItem
            Dim i As Integer
    
            For i = 0 To ListView1.Items.Count - 1
                ListView1.Items(i).Selected = False
                ListView1.Items(i).BackColor = Color.White
            Next
    
            With ListView1
                itm = .FindItemWithText(txtSearch.Text, True, 0, True)
    
                If Not itm Is Nothing Then
    
                    .Items.Item(itm.Index).BackColor = Color.Green
                    .Items.Item(itm.Index).EnsureVisible()
                Else
                    MsgBox("No Record Found!")
                    For i = 0 To ListView1.Items.Count - 1
                        ListView1.Items(i).Selected = False
                        ListView1.Items(i).BackColor = Color.White
                    Next
                    .Items(0).EnsureVisible()
                    .Items.Item(0).BackColor = Color.BurlyWood
    
                    txtSearch.SelectionStart = 0
                    txtSearch.SelectionLength = Len(txtSearch.Text)
                    txtSearch.Focus()
                End If
            End With
            itm = Nothing
        End Sub
    Quote Originally Posted by dday9 View Post
    While I've never used a ListView(seriously, I think they're useless)
    January 15, 2015 is the day that you broke my heart, dday.

    Anyhow OP, You can loop through a listview looking at the subitem:

    Code:
            For Each itm As ListViewItem In ListView1.Items
                MsgBox(itm.SubItems(0).Text) 'get column 0
            Next itm
    So you can look through each item to find what you are looking for, turn this into a function where you supply a column number that you need to search and the text you are searching for:

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,954

    Re: How to modify ListView search code to search in ListView Column 5, instead of Col

    FindItemWithText - does just what it says... finds the item with the text... it is not FindSubItemWithText ... that's why it failed ... the startindex is the starting index of the ITEM to find... not the column/subitem. The ListView has multiple views, and the subitems don't apply to most of them - but the text is always displayed. That's why FindItemWithText only looks at col 0. Actually, it's looking at the the text of each items.

    To search through the subitems, you're going to have to roll your own, like what jay did.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,954

    Re: How to modify ListView search code to search in ListView Column 5, instead of Col

    as for the useless comment - it depends. as a display tool where you want to do some simple formatting or grouping, or allow the different views, they're great. For displaying and manipulating data, they suck. I only use the ListView in limited caseswhere I jsut want to quickly display some text - I used it in a utility I built to monitor files in a folder. When it starts up, it scans the folder, puts the items into an LV... then when the FileSystemWatcher fires because the file changed, it find the file, bolds it and changes it to green... now I can see that it will be deployed ... after deployment, it changes back to normal.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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