Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ListView Custom Search Filter - List(Of T) Method
#1
I also have a predicate method, but this works well and should be easy to understand, for those of you who leech and probably don't understand this, well I hope it works for you lol, and to those that don't understand but want to know more about my code, just reply and i'll respond.

Code:
Public Class Form1

    Public SearchText As String
    Public _data() As String = {"1_one,1_two,1_three", "2_one,2_sdf,2_three", "3_one,3_sdf,3_three"}
    Public Arr As New List(Of String)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Arr.AddRange(_data)

        For Each obj As String In Arr
            Dim Sdata() As String = obj.Split(","c)
            Dim LVC As New ListViewItem(Sdata)
            ListView1.Items.Add(LVC)
        Next

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SearchText = TextBox1.Text

        Dim Arr_Modified As New List(Of String)
        Arr_Modified.AddRange(Arr)

        For Each ObjNewStr As String In Arr
            'If not middle value in array contains search value
            If Not ObjNewStr.Split(","c)(1).Contains(SearchText) Then
                Arr_Modified.Remove(ObjNewStr)
            End If
        Next

        ListView1.Items.Clear()
        For Each ObjNewStr As String In Arr_Modified
            Dim StrSplit() As String = ObjNewStr.Split(","c)
            Dim Mod_LVC As New ListViewItem(StrSplit)
            ListView1.Items.Add(Mod_LVC)
        Next

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        SearchText = TextBox1.Text

        Dim Arr_Modified As New List(Of String)
        Arr_Modified.AddRange(Arr)

        For Each ObjNewStr As String In Arr
            'If not middle value in array contains search value
            If Not ObjNewStr.Split(","c)(1).Contains(SearchText) Then
                Arr_Modified.Remove(ObjNewStr)
            End If
        Next

        ListView1.Items.Clear()
        For Each ObjNewStr As String In Arr_Modified
            Dim StrSplit() As String = ObjNewStr.Split(","c)
            Dim Mod_LVC As New ListViewItem(StrSplit)
            ListView1.Items.Add(Mod_LVC)
        Next
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'Test Button for Debugging
    End Sub
End Class
Reply
#2
What does this do exactly Ace? I would test it out but I don't have VB.NET since I formatted. Sad

P.S Good to see you're back? Big Grin
Reply
#3
I still administer Tech.Reboot.Pro, so that's still my first priority, but I am not on a indefinite absence anymore here. I just won't bother with any debates on the Support Feather or anything to do with HF on this forum. It's a waste of my time. You'll still see all my latest greatest codes and releases and other stuff on Tech.Reboot.Pro. I use that site exclusively for all my best stuff to share with the members there Smile

What this does, is defines a List of type string, formatting each item string as a listview item in the Listview collection based on the search text which is what gets compared to with the List of string which further filters the Listview collection.

Much better than using an array because of the methods contained in List(of T) and furthermore List(of T) can deal with larger data, and retrieve data faster than looping through an array. It's also a memory saver in comparison to an arraylist.

I tried using predicate in my newest method, but it's not yet compatible to work with subitems as columns more than 1 in a listview control if you want to get or search through items in a specific column, whereas this is fully customizable for that.

Edit: I noticed I got removed from my one and only Geek Squad though eventually at some point lol Smile
Reply
#4
Not bad at all dude, thankyou.
Reply
#5
No problem, I haven't posted the one i've bound with Predicate here because that method I didn't finish off yet, so it only works with the first column in reality, but this one should work for every last item in the listview and columns included.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [NEED HELP] how to read a text file and import to listview vb.net w00pz 7 5,788 02-13-2012, 06:42 AM
Last Post: AceInfinity
  Drag and Drop to Listview [Tutorial] AceInfinity 3 2,687 09-03-2011, 05:29 AM
Last Post: .edit
  [Help] - ListView Control Fragma 2 982 08-19-2011, 05:10 AM
Last Post: Fragma

Forum Jump:


Users browsing this thread: 1 Guest(s)