Pregunta

I've tried searching on stackoverflow and implementing code that I found but to no avail. As the text changes I want to use the current text in the textbox to filter through the listview items (so the items that are not closed to being matched get removed) and it only leaves me with whatever is contained in the columns.

Here's an example of what I mean:

Search: "Georges"

|1|Anderson Silva|Anderson silva is the champion in the ...|

|2|Georges St-Pierre| Georges is the champion in the ...|

|3|Georges Sotoropolis| Georges Sotoropolis is a fighter in the lightweight division|

With this search, only rows 2 and three would be returned. The first row would be omitted and not displayed. Once I erase the terms, then it would get displayed.

Here is the code that I currently have:

Private Sub tbSearch_TextChanged(sender As Object, e As System.EventArgs) Handles tbSearch.TextChanged
    lwArticles.BeginUpdate()

    If tbSearch.Text.Trim().Length = 0 Then
        'Clear listview
        lwArticles.Clear()

        'If nothing is in the textbox make all items appear
        For Each item In originalListItems
            lwArticles.Items.Add(item)
        Next
    Else
        'Clear listview
        lwArticles.Clear()

        'Go through each item in the original list and only add the ones which contain the search text
        For Each item In originalListItems
            If item.Text.Contains(tbSearch.Text) Then
                lwArticles.Items.Add(item)
            End If
        Next
    End If
    lwArticles.EndUpdate()

End Sub

It seems to be working but I cannot see the listview items once I enter something in the tbSearch. The scrollbar gets smaller / larger depending if there are more / less items due to the search being executed. My problem seems that they are not visible

Thank you!

¿Fue útil?

Solución

Listview.Clear wipes out the items, columns, and groups. It appears you only want to wipe out the items, so call lwArticles.Items.Clear instead of lwArticles.Clear

Otros consejos

I would suggest another approach - first create a DataTable based on your original items. Then create a DataView and assign that as DataSource of your ListView. Now you can change DataView.RowFilter, and your list will update automatically, so only items matching the filter will show up. Using this approach you don't need to recreate everything from scratch every time, and your TextChanged becomes very simple and maintainable - it just changes RowFilter, if a corresponding DataView has already been created.

Here's the final answer of my question:

Private originalListItems As New List(Of ListViewItem) 'this gets populated on form load

Private Sub tbSearch_TextChanged(sender As Object, e As System.EventArgs) Handles tbSearch.TextChanged
    lwArticles.BeginUpdate()

    If tbSearch.Text.Trim().Length = 0 Then
        'Clear listview
        lwArticles.Items.Clear()

        'If nothing is in the textbox make all items appear
        For Each item In originalListItems
            lwArticles.Items.Add(item)
        Next
    Else
        'Clear listview
        lwArticles.Items.Clear()

        'Go through each item in the original list and only add the ones which contain the search text
        For Each item In originalListItems
            If item.Text.Contains(tbSearch.Text) Then
                lwArticles.Items.Add(item)
            End If
        Next
    End If

    lwArticles.EndUpdate()
End Sub

Credits to Dan-o for explaining that lwArticles.Clear() will clear everything. He notified me that I needed lwArticles.Items.Clear() to clear only the items in the listview.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top