2012-07-12 54 views
0

當我希望鍵入列表框項目的名稱(列表框由目錄中的文件填充)時,例如,如果我鍵入「蘋果」,按A將帶我到第一個帶有A的對象在它的名字中,但在輸入「p」之後會將我帶到第一個字母爲p的第一個字母。有沒有什麼辦法可以讓我可以輸入幾個字符,並且會把我帶到那個特定的項目上?例如,列表可能有;在列表框中鍵入文件名

ability 
idea 
boring 

打字 「ABI」 將選擇的能力,而不是 「能力」,那麼 「無聊」,然後在 「想法」

任何幫助表示讚賞。謝謝。

回答

2

我認爲可以使System.Windows.Forms.ListBox的行爲像這樣,但它需要一些非平凡的代碼才能使它工作。 System.Windows.Forms.ListView具有內置的這種行爲,所以我建議使用ListView而不是ListBox。

' Hide the headers to make the ListView look like a ListBox. 
Me.ListView1.View = View.Details 
Me.ListView1.HeaderStyle = ColumnHeaderStyle.None 

Me.ListView1.BeginUpdate() 
Try 
    ' System.Windows.Forms.ListView doesn't have data binding capability. 
    ' The listview's items have to be added using its 
    ' Items.Add, Items.AddRange or Items.Insert methods. 
    For Each filename As String In Directory.GetFiles("C:\Windows").Select(Function(s) Path.GetFileName(s)) 
    Me.ListView1.Items.Add(filename) 
    Next 
Finally 
    Me.ListView1.EndUpdate() 
End Try 

' Add the column after adding the items. 
' Setting column width to -1 will make 
' the column autosize itself to the longest item. 
Dim columnHeader As New ColumnHeader 
columnHeader.Width = -1 
Me.ListView1.Columns.Add(columnHeader) 
+0

你如何給某人代表?因爲你的代碼像魅力一樣工作!非常感謝你,我現在一直在這裏。 :D – 2012-07-12 18:25:13

+0

@Matt:這是常見問題解答條目(http://stackoverflow.com/faq#reputation)。當你接受答案時,你給了我點數。謝謝! – 2012-07-12 23:47:57