EDIT2:您需要将您的逻辑(您的人员,日期)与您的演示文稿(您通过放入列表框中向用户展示的内容)分开。您应该始终避免的事情是显示某些内容(例如,显示人员列表),然后将其读回并尝试理解它。
Module MyForm
' Keep our dates & people in this dictionary
' Because it's a SortedDictionary, it'll keep them sorted by key
' key, value
Public personDictionary As New SortedDictionary(Of DateTime, Person)
Public Sub New()
InitializeComponent()
' Call CreatePeople() to fill our dictionary when the form is made
CreatePeople()
' Then call FillListBox() to fill our listbox from that dictionary
FillListBox()
End Sub
Private Sub CreatePeople()
' Make your persons and dates here
Dim a = New Person(...)
Dim dateA = New DateTime(2012,2,3)
' Keep them in our internal dictionary
' .Add(..) takes our key (a DateTime) and a value (a Person)
personDictionary.Add(dateA, a)
End Sub
Private Sub FillListBox()
lstFDisplay.Clear()
' Here we do something 'For Each' item in the dictionary
' The dictionary is filled with 'pairs' of key|value (DateTime|Person)
For Each pair In personDictionary
DateTime date = pair.Key
Person person = pair.Value
'Use this data to add items to our UI
lstFDisplay.Items.Add("Name: "&person.Name
lstFDisplay.Items.Add("Address: "&person.Address
lstFDisplay.Items.Add(person.Name&" registered on "&date)
Next
End Sub
当您想添加或删除人员时,只需.Add(..)
或.Remove(..)
从字典FillListBox()
中再次调用以刷新您的 UI。通过将值保留在代码本身中,而不是每次都从列表框中重新读取它,您可以更好地控制如何处理该信息。