2009-12-30 62 views
0

我是在.net中开发UI的新手。基本上我使用的是一个列表视图,我想在列表视图中搜索项目。 假设列表中包含这样的:如何在没有发现字符串的情况下在listview中搜索所有项目

斯诺名称
1迈克尔·杰克逊
约翰二米切尔

如果我搜索usign应该显示所有符合条件的项目中第二个或第三个任期。 我试过使用.FindString,但它只是搜索第一项。这不是我想要的。任何人都可以告诉我一个更好的方式来做我想做的事吗?

回答

1

只要重复调用ListBox.FindString(),直到找到它们全部。例如:

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 

如果你需要找到列表框项目字符串的任何部分匹配,那么你可以搜索这样的项目:

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 
相关问题