How can one bind HashTable keys to a ListBox in VB.NET?
I'm helping a friend port some code that I have written in 开发者_JS百科Java to Visual Basic.NET. VB.NET is not my language of choose, so I am entirely new to it's quirks. How can I display keys from a HashTable in a ListBox?
Thanks in advance,
Sean W.
For Each key As Object In myHashTable.Keys
ListBox1.Items.Add(key.ToString)
Next
or
Dim enumerator As IDictionaryEnumerator = myHashTable.GetEnumerator()
While enumerator.MoveNext
ListBox1.Items.Add(enumerator.Key)
End While
But you should use generic dictionaries instead that are type safe and therefore faster and less errorphrone.
Dim myDictionary As New Dictionary(Of Int32, String)
For i As Int32 = 1 To 1000
myDictionary.Add(i, i & ". Entry")
Next
For Each key As String In myDictionary.Keys
ListBox1.Add(key)
Next
The Keys
property on your Hashtable
should be bindable directly to the Datasource
property on your Listbox
精彩评论