2012-08-11 44 views
0

我有一个问题,我需要理清在列表框中的项目,我知道该怎么做,但问题是,我的列表框整理出这样排序列表框,但保持项目组合在一起

人1姓名
第1个人姓
人1日
第1个人性别
第1个人地址
人2姓名
人2姓
人2日期
人2性别
人2地址
随着每个值在一个新的行。 我想要的是将每个人的5个细节放在一起并按日期排序,但我不知道该怎么做,因为我知道的唯一排序细节会混合所有数据。

Private Sub btnSortDate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSortDate.Click 
    Dim arr As New ArrayList 
    Dim iLp As Integer 

    arr.AddRange(lstFDisplay.Items) 

    arr.Sort() 

    lstFDisplay.Items.Clear() 

    For iLp = 0 To arr.Count - 1 
     lstFDisplay.Items.Add(arr(iLp)) 
    Next iLp 
End Sub 

回答

0

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。通过将值保存在代码本身中,而不是每次都从列表框重新读取它,您可以更好地控制如何处理这些信息。

+0

谢谢你的回答,我认为你在vb上比我有更多的知识,所以谢谢你把我推向了正确的方向。 – nurafh 2012-08-11 14:34:45

+0

这是一个自定义类,因为我认为这可能是需要的。 '公共类Person 公共属性名字作为字符串 公共属性名字作为字符串 公共财产性别作为字符串 公共财产applicationdate截止日期 公共物业地址作为字符串 的Public Sub New(BYVAL名字作为字符串,可选BYVAL姓氏的String = “None”,可选ByVal gender As String =“None”,可选ByVal applicationdate As Date = Nothing,可选ByVal地址为String =“None”)' – nurafh 2012-08-11 14:45:02

+0

'_firstname = firstname _lastname = lastname _gender = gender _applicationdate = applicationdate _address = address End Sub Public Overrides Function ToString()As String 返回String.Format(“{0},{1},{2}”,Me.lastname,Me.firstname,Me.applicationdate) End Function End Class' – nurafh 2012-08-11 14:46:07

相关问题