わかりましたので、全面的なオーバーホールを行っています。あなたのおかげで私は何ヶ月もきちんと働いています!しかし、あなたが言ったように、私はこれを修正して、よりよく理解しようとする必要があります. そこで、テストするものを含む 2 つのリスト ボックスを追加しようとしました。このプログラムは現在、マイルストーン (歳から 10 歳から 100 歳まで) をテストしています。コードを編集しましたが、月のテストから年に変更するためにチェックする行がわかりません。したがって、追加した新しいリスト ボックスには、月のテストと同じ情報が表示されます。たとえば、John Doe 2003 年 4 月 9 日は「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