2012-07-11 35 views
0

我試圖使TextBox1成爲搜索欄,以搜索ListBox1中的特定字符串。字符串搜索列表框,數據源屬性集

我希望它刪除沒有我搜索的字符串的其他項目。該列表顯示了特定目錄中的所有文件,因此如果我搜索「icon_」它會只有顯示文件與icon_在名稱中。這可能嗎?

我問這個問題,前一段時間,但由於列表框是由文件名從一個特定的目錄,這給了我這個錯誤填充我不能使用任何的答案:

Items collection cannot be modified when the DataSource property is set.

回答

0

許多不同的方式來做到這一點。

此方法將文件列表放到DataTable中並使用BindingSource,您可以使用它的Filter屬性來過濾列表。

這裏是一個列表框和文本框形式:

Public Class Form1 
    Dim bs As New BindingSource 

    Public Sub New() 
    InitializeComponent() 
    End Sub 

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs) 
    MyBase.OnLoad(e) 

    Dim testPath As String = "c:\MyPath" 
    Dim dt As New DataTable 
    dt.Columns.Add("File", GetType(String)) 

    For Each f As String In Directory.GetFiles(testPath) 
     Dim row As DataRow = dt.NewRow 
     row("File") = Path.GetFileName(f) 
     dt.Rows.Add(row) 
    Next 
    bs.DataSource = dt 

    ListBox1.DisplayMember = "File" 
    ListBox1.ValueMember = "File" 
    ListBox1.DataSource = bs 
    End Sub 

    Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.TextChanged 
    bs.Filter = String.Format("File LIKE '*{0}*'", TextBox1.Text) 
    End Sub 

End Class 
0

DataSource屬性設置,您不能修改該列表。儘管使用DataSource屬性填充ListBox控件很方便,但它當然不是必需的。您可以使用其Items.Add方法將項目添加到控件。舉例來說,從我的回答借用你剛纔的問題:

Public Class FileSearchTool 
    Public Sub New(ByVal listBox As ListBox, ByVal textBox As TextBox) 
     _listBox = listBox 
     _textBox = textBox 
    End Sub 

    Private _listBox As ListBox 
    Private WithEvents _textBox As TextBox 
    Private _fileNames As New List(Of String)() 
    Private _folderPath As String 

    Public Property FolderPath() As String 
     Get 
      Return _folderPath 
     End Get 
     Set(ByVal value As String) 
      _folderPath = value 
      loadFilePaths() 
     End Set 
    End Property 

    Private Sub loadFilePaths() 
     _fileNames = New List(Of String)(Directory.GetFiles(_folderPath)) 
     refreshList() 
    End Sub 

    Private Sub _textBox_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles _textBox.TextChanged 
     refreshList() 
    End Sub 

    Private Sub refreshList() 
     _listBox.SuspendLayout() 
     _listBox.Items.Clear() 
     For Each item As String In _fileNames 
      If item.StartsWith(_textBox.Text, StringComparison.CurrentCultureIgnoreCase) Then 
       _listBox.Items.Add(item) 
      End If 
     Next 
     _listBox.ResumeLayout() 
    End Sub 
End Class 

然後以任何形式,你可以使用這樣的:

Public Class Form1 
    Private _tool As FileSearchTool 
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load 
     _tool = New FileSearchTool(ListBox1, TextBox1) 
     _tool.FolderPath = "C:\" 
    End Sub 
End Class 

但是,在這一點上,你可能也只是將其封裝進一步通過創建一個FileSearch用戶控件。

或者,正如我在回答你剛纔的問題說,如果你想要的是一個自動完成框,你可以只使用文本框沒有一個列表框都這樣:

Dim source As New AutoCompleteStringCollection() 
source.AddRange(Directory.GetFiles("C:\")) 
TextBox1.AutoCompleteCustomSource = source 
TextBox1.AutoCompleteMode = AutoCompleteMode.Suggest 
TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource 

在事實上,另一個有趣的選擇是,你可以設置AutoCompleteSourceFileSystem,你可能想玩。