好的,所以我正在进行全面检修。多亏了你,我才能正常工作几个月!但正如你所说,我应该修改它以更好地理解。所以我尝试添加另外 2 个列表框来测试。该计划现在正在测试里程碑(年龄 - 10 岁到 100 岁)。我编辑了代码,但我不知道要检查哪一行才能从测试月份更改为年份?所以我添加的新列表框显示了与月份测试相同的信息,而不是我想要完成的内容。例如:John Doe 4/9/2003 将出现在“10”里程碑中。
Private Sub lbMonth_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lbMonth.SelectedIndexChanged
If lbMonth.SelectedIndex < 0 Then Return
lbPerson.Items.Clear()
Dim index As Integer = lbMonth.SelectedIndex
For Each ele In Birthdays(index + 1)
lbPerson.Items.Add(ele)
Next
End Sub
Private Sub lbMilestone_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lbMilestone.SelectedIndexChanged
If lbMilestone.SelectedIndex < 0 Then Return
lbPerson2.Items.Clear()
Dim index As Integer = lbMilestone.SelectedIndex
For Each ele2 In Birthdays2(index)
lbPerson2.Items.Add(ele2)
Next
End Sub
Private Birthdays(12) As List(Of String)
Private Birthdays2(10) As List(Of String)
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
'initialize the Month list
lbMonth.Items.Clear()
lbMonth.Items.Add("January")
lbMonth.Items.Add("February")
lbMonth.Items.Add("March")
lbMonth.Items.Add("April")
lbMonth.Items.Add("May")
lbMonth.Items.Add("June")
lbMonth.Items.Add("July")
lbMonth.Items.Add("August")
lbMonth.Items.Add("September")
lbMonth.Items.Add("October")
lbMonth.Items.Add("November")
lbMonth.Items.Add("December")
lbMilestone.Items.Clear()
lbMilestone.Items.Add("10")
lbMilestone.Items.Add("20")
lbMilestone.Items.Add("30")
lbMilestone.Items.Add("40")
lbMilestone.Items.Add("50")
lbMilestone.Items.Add("60")
lbMilestone.Items.Add("70")
lbMilestone.Items.Add("80")
lbMilestone.Items.Add("90")
lbMilestone.Items.Add("100")
'initialize the Lists (Instance required in order to access each list-object)
For i As Integer = 0 To 12
Birthdays(i) = New List(Of String)
Next
For j As Integer = 0 To 10
Birthdays2(j) = New List(Of String)
Next
'load some birthdays
Dim filename As String = Application.StartupPath + "\Birthday.txt"
If Not My.Computer.FileSystem.FileExists(filename) Then Throw New Exception("Filename """ + filename + """ does not exist!")
Dim fileContent As String = My.Computer.FileSystem.ReadAllText(filename)
Dim lines() As String = Split(fileContent, vbCrLf)
For Each ele As String In lines
Dim line As String = ele.Trim
Dim datePos As Integer = line.LastIndexOf(vbTab) 'find last space between name and date
If datePos < 5 Then Continue For 'if full name is less than 5 chars, then it probably not a line with an entry
Dim dateString As String = Mid(line, datePos + 2) 'all after that last space is date
Dim name As String = Mid(line, 1, datePos).Trim ' all before that last space is name
'Dim birthday As Date = Convert.ToDateTime(parts(1).Trim) ' used this conversion before, but lets try the other way
Dim birthday As Date
Try
birthday = DateTime.ParseExact(dateString, "M/d/yyyy", System.Globalization.CultureInfo.GetCultureInfo("en-US"))
Catch ex As Exception
Continue For
End Try
Dim month As Integer = birthday.Month
Dim year As Integer = CInt(Date.Now.Subtract(birthday).TotalDays / 365 / 10)
Birthdays(month).Add(name)
Birthdays2(year).Add(name)
Next
End Sub