1

わかりましたので、全面的なオーバーホールを行っています。あなたのおかげで私は何ヶ月もきちんと働いています!しかし、あなたが言ったように、私はこれを修正して、よりよく理解しようとする必要があります. そこで、テストするものを含む 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
4

2 に答える 2

0

新しいプロジェクトを作成し、2 つのリストボックスを追加します。1 つのリストボックスにlbMonthという名前を付け、もう1 つのリストボックスにlbPersonという名前を付けます

次に、次のコードを挿入します。

Private Sub lbMonth_SelectedIndexChanged(sender As Object, 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 Birthdays(12) As List(Of String)

Private Sub Form1_Load(sender As Object, 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")

    '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

    'load some birthdays
    Dim filename As String = Application.StartupPath + "\names.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(" ") '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
        Birthdays(month).Add(name)
    Next

End Sub

誕生日のファイル名を「names.txt」に変更し、おそらくそれを project/bin/Debug か、プログラム実行可能ファイルを実行する他の場所に配置する必要があります。

例外は、正しいパスを見つけるのに役立ちます。

アップデート

両方のリストボックスが比較的大きいことを確認してください (12 か月すべてを表示できます)。

プログラムをテストします。月 7 または 8 を選択すると、何かが起こるはずです (リストした 3 つのエントリを使用する場合)。

Update2

ファイルに空の行/無効なデータを含む行が含まれている場合、例外を修正するために Try..Catch ブロックを追加してコードを変更しました。

プロジェクトのコードを置き換えるだけです。

また、テキスト ファイルで使用されているのは、タブではなく " " スペースであることを確認してください。タブが使用されている場合は、置き換えます

Dim datePos As Integer = line.LastIndexOf(" ")

Dim datePos As Integer = line.LastIndexOf(vbTab)

今回は頑張ってください:)それでも失敗する場合は、文字列を関数に変換してもう少しテストするか、3つの数値すべてを取得してから、日で新しい日付を作成することで、さらに手動で行うことができます、月、および年 (それぞれ整数)

Update3

Birthday 配列を 13 フィールドに変更しました (0 から 12、0 は使用されなくなりました)。

全13フィールドも初期化するように変更

1 月はインデックス 0、2 月はインデックス 1 などであるため、インデックス + 1 から読み取るように変更されました。

于 2013-04-09T01:12:51.403 に答える