1

好的,所以我正在进行全面检修。多亏了你,我才能正常工作几个月!但正如你所说,我应该修改它以更好地理解。所以我尝试添加另外 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
4

2 回答 2

0

创建一个新项目并添加 2 个列表框。命名一个列表框lbMonth,另一个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 个条目)

更新2

如果文件包含包含无效数据的空行/行,我已经通过添加 Try..Catch 块来修复异常来修改代码。

只需替换项目中的代码。

还请确保在文本文件中使用的是 " " 空格,而不是制表符。如果使用制表符,则替换

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

Dim datePos As Integer = line.LastIndexOf(vbTab)

这次祝你好运 :) 如果它仍然失败,那么我们可以将字符串转换为函数并进行更多测试,或者甚至通过获取所有 3 个数字来手动进行更多操作,然后创建一个新的日期日期, 月, 年 (每个都是整数)

更新3

将生日数组更改为具有 13 个字段(0 到 12,0 不再使用)

更改为也初始化所有 13 个字段

更改为从 index+1 读取,因为 1 月为 index 0,2 月为 index 1 等。

于 2013-04-09T01:12:51.403 回答
0

因此,让我们更改以下内容(顺便说一句。使用 days/365 的好主意):

    Dim month As Integer = birthday.Month
    Dim year As Integer = CInt(Date.Now.Subtract(birthday).TotalDays / 365)
    Birthdays(month).Add(name)

你想有 10 年的步数,所以将年份除以 10

    Dim month As Integer = birthday.Month
    Dim year As Integer = CInt(Date.Now.Subtract(birthday).TotalDays / 365 / 10)
    Birthdays(month).Add(name)

而且您还需要将计算出的年份信息用于某事..让我们填写另一个字段:)

    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)

现在检查另一部分..

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 Birthdays(index + 1)
        lbPerson2.Items.Add(ele2)
    Next
End Sub

大部分是正确的,但是您从“旧”月份列表中读取

    For Each ele2 In Birthdays(index + 1)

所以将行更改为

    For Each ele2 In Birthdays2(index + 1)

而且我认为 10 应该代表年龄 0-9 岁,因此如果单击第一个元素(索引 = 0),则应返回来自 Birthdays2(0) 的人员。(= 删除+ 1

    For Each ele2 In Birthdays(index)

因此该方法应如下所示:

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 Birthdays(index)
        lbPerson2.Items.Add(ele2)
    Next
End Sub

更新

更改方法以使用另一个子:

       [...]
        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
        AddUser(name, dateString)
    Next

End Sub

这里是子:(您看到它几乎是 1:1,除了Exit 子,因为没有 for 循环可以退出,但由于该方法包含执行到循环结束的所有代码,因此是一个退出子这里的作用与以前版本中的continue相同。

Private Sub AddUser(Name As String, dateString As String)
    Dim birthday As Date
    Try
        birthday = DateTime.ParseExact(dateString, "M/d/yyyy", System.Globalization.CultureInfo.GetCultureInfo("en-US"))
    Catch ex As Exception
        Exit Sub
    End Try

    Dim month As Integer = birthday.Month
    Birthdays(month).Add(Name)

    Dim year As Integer = CInt(Date.Now.Subtract(birthday).TotalDays / 356 / 10)
    Birthdays2(year).Add(Name)
End Sub

现在当用户输入新数据时,您只需要调用该方法

AddUser("new name", "1/2/1934")

或带变量

AddUser(tbYour_name_textbox, tbYour_birthday_date_textbox)
于 2013-04-10T01:50:55.037 回答