2009-12-30 5 views
0

Je suis nouveau à développer l'interface utilisateur en .net. Fondamentalement, j'utilise un listview et je veux faire une recherche parmi les éléments de listview. Supposons que la liste contient ceci:comment rechercher tous les articles dans listview sans findstring

Sno Nom
1 Michael Jackson
2 John Mitchel

Si je recherche usign la deuxième ou troisième terme, il devrait afficher tous les éléments qui correspondent aux critères. J'ai essayé d'utiliser .FindString, mais il ne fait que rechercher le premier terme. Ce n'est pas ce que je veux. Quelqu'un peut-il me dire une meilleure façon de faire ce que je veux?

Répondre

1

Appelez simplement à plusieurs reprises ListBox.FindString() jusqu'à ce que vous les ayez tous trouvés. Par exemple:

Public Class Form1 
    Public Sub New() 
     InitializeComponent() 
     ListBox1.SelectionMode = SelectionMode.MultiExtended 
    End Sub 

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged 
     ListBox1.BeginUpdate() 
     ListBox1.SelectedIndices.Clear() 
     If TextBox1.Text.Length > 0 Then 
      Dim index As Integer = -1 
      Do 
       dim found As integer = ListBox1.FindString(TextBox1.Text, index) 
       If found <= index Then Exit Do 
       ListBox1.SelectedIndices.Add(found) 
       index = found 
      Loop 
     End If 
     ListBox1.EndUpdate() 
    End Sub 
End Class 

Si vous avez besoin de trouver une correspondance sur une partie de la zone de liste chaîne de l'article, vous pouvez chercher les articles comme celui-ci:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged 
    ListBox1.BeginUpdate() 
    ListBox1.SelectedIndices.Clear() 
    If TextBox1.Text.Length > 0 Then 
     For index As Integer = 0 To ListBox1.Items.Count - 1 
      Dim item As String = ListBox1.Items(index).ToString() 
      If item.IndexOf(TextBox1.Text, StringComparison.CurrentCultureIgnoreCase) >= 0 Then 
       ListBox1.SelectedIndices.Add(index) 
      End If 
     Next 
    End If 
    ListBox1.EndUpdate() 
End Sub 
Questions connexes