Search Listview?

Do you need something made? then ask in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
10 posts Page 1 of 1
Contributors
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1892
Joined: Wed Dec 16, 2009 9:56 pm

Search Listview?
zachman61
Hello,
Im trying to know what would be a listview version of code that Usman55 posted
Code: Select all
Dim Contact As String = TextBox1.Text.ToString()
        Dim Index As Integer = ListBox1.FindString(Contact)
        If Index = -1 Then
            ListBox1.SelectedIndex = ListBox1.SelectedIndex
        Else
            ListBox1.SetSelected(Index, True)
        End If
which is under Textbox1_textchanged
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
User avatar
Codex
Coding God
Coding God
Posts: 2028
Joined: Wed Mar 31, 2010 5:50 pm

Re: Search Listview?
Codex
Do you wanna search subitems too ?
We shall let the revolution begin.. the revolution for freedom, freedom against censorship. We shall fight in the fields and in the streets, we shall fight in the hills; we shall never surrender
User avatar
Axel
Coding God
Coding God
Posts: 1928
Joined: Sun Jun 27, 2010 9:15 pm

Re: Search Listview?
Axel
Code: Select all
Shared function Searchlistview as boolean(ByVal stringtosearch as string)
For I as integer =0 To listview.Items.Count -1
For X as integer =0 To listview.Items(I).Subitems.Count -1
If listview.Items(I).Subitems(X).Text.Contains(stringtosearch) Then
listview.Items(I).Selected = true
return true
end if
Next
Next
End function
Not sure if this works , but you may give it a try ;)
Oh and I doubt about "Subitems(X).Text.Contains(stringtosearch)" don't know if that's right
http://vagex.com/?ref=25000
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Re: Search Listview?
GoodGuy17
Try:
Code: Select all
Dim sItems As Integer
sItems = ListView1.SubItems.Count
For i = 0 To sItems - 1
If ListView1.Items.Item(i).Contains(string) Then
MsgBox("Found at Item " & i)
Elseif ListView1.Items.Item(i).SubItem(i).Contains(string) Then
MsgBox("Found at Subitem " & i)
End If
Next
Not sure if it will work, tell me if it does.
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Search Listview?
mandai
Axel's code seems to contain some syntax errors in the function declaration.
Reboh's code doesn't work as there is no SubItems object in the listview control.

If you only want to search the first column then the code should look like this:
Code: Select all
        Dim found As Integer = -1
        For i As Integer = 0 To ListView1.Items.Count - 1
            If ListView1.Items(i).Text.Contains(txtFind.Text) Then
                found = i
                Exit For
            End If

        Next

        If found > -1 Then
            ListView1.Items(found).Selected = True
            ListView1.Select()
        End If
Last edited by mandai on Mon Apr 18, 2011 9:04 pm, edited 1 time in total.
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Re: Search Listview?
GoodGuy17
mandai wrote:
Axel's code seems to conatin some syntax errors in the function declaration.
Reboh's code doesn't work as there is no SubItems object in the listview control.
Axel's code contains .SubItems after a full-stop. Mine contains .SubItem. You said that I used SubItems, but I didn't. Why doesn't mine work (as in, what error?)
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Search Listview?
mandai
I have attached a screenshot.
You do not have the required permissions to view the files attached to this post.
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Re: Search Listview?
GoodGuy17
Oh, I see. I didn't write my code in VB :P I just mocked it up before school in the morning :D
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1892
Joined: Wed Dec 16, 2009 9:56 pm

Re: Search Listview?
zachman61
Codex wrote:
Do you wanna search subitems too ?
Yes that would be Great :)
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Search Listview?
mandai
You could use this to search the whole listview (including subitems):
Code: Select all
    Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click

        Dim found As Integer = -1
        For row As Integer = 0 To ListView1.Items.Count - 1
            For column As Integer = 0 To ListView1.Items(row).SubItems.Count - 1

                If ListView1.Items(row).SubItems(column).Text.Contains(txtFind.Text) Then
                    found = row
                    Exit For
                End If
            Next

            If found > -1 Then
                Exit For
            End If

        Next

        If found > -1 Then
            ListView1.Items(found).Selected = True
            ListView1.Select()
        End If

    End Sub
It will stop searching after it finds 1 item.
10 posts Page 1 of 1
Return to “Tutorial Requests”