我認爲可以使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)
你如何給某人代表?因爲你的代碼像魅力一樣工作!非常感謝你,我現在一直在這裏。 :D – 2012-07-12 18:25:13
@Matt:這是常見問題解答條目(http://stackoverflow.com/faq#reputation)。當你接受答案時,你給了我點數。謝謝! – 2012-07-12 23:47:57