Member Avatar for Micheal87

Hi, I have a issues, I have to load again the listbox, after filtering trought a textbox if a string is the same. I tried this code, but doesn't work at the is null or empthy, how can I reload it by a cicle for?

 Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
        Dim items = From it In ListBox1.Items.Cast(Of Object)()
                    Where it.ToString().IndexOf(TextBox3.Text, StringComparison.CurrentCultureIgnoreCase) >= 0
        Dim matchingItemList As List(Of Object) = items.ToList()
        ListBox1.BeginUpdate()
        ListBox1.Items.Clear()
        For Each item In matchingItemList
            ListBox1.Items.Add(item)
        Next
        If String.IsNullOrEmpty(TextBox3.Text) Then
            ListBox1.Items.AddRange(My.Settings.userlist.ToString)
        End If
        ListBox1.EndUpdate()
    End Sub
Member Avatar for Micheal87

Solution: (for me working, also you need to put these in the textbox textchangevent, you can reach it by double clicking on it by form)

Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
        Dim items = From it In ListBox1.Items.Cast(Of Object)()
                    Where it.ToString().IndexOf(TextBox3.Text, StringComparison.CurrentCultureIgnoreCase) >= 0
        Dim matchingItemList As List(Of Object) = items.ToList()
        ListBox1.BeginUpdate()
        ListBox1.Items.Clear()
        For Each item In matchingItemList
            ListBox1.Items.Add(item)
        Next
        If String.IsNullOrEmpty(TextBox3.Text) Then
            ListBox1.Items.Clear()
            For Each i In My.Settings.userlist
                ListBox1.Items.Add(i)
            Next
        End If
        ListBox1.EndUpdate()
    End Sub
End Class

I would do the following:

    Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
        Dim items = (From it In ListBox1.Items Select it
                     Where it.ToString().IndexOf(TextBox3.Text, 
                     StringComparison.CurrentCultureIgnoreCase) >= 0).ToArray
        ListBox1.BeginUpdate()
        ListBox1.Items.Clear()
        For Each item As Object In items
            ListBox1.Items.Add(item)
        Next
        If String.IsNullOrEmpty(TextBox3.Text) Then
            ListBox1.Items.AddRange(My.Settings.userlist.ToString)
        End If
        ListBox1.EndUpdate()
    End Sub

Of course the for-next can be replaced by an AddRange

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.