从文本文件导入数据

时间:2013-04-08 23:18:50

标签: vb.net

好的,所以我正在全面检修。多亏了你,我让这几个月正常工作!但正如你所说,我应该修改这个以试图更好地理解。所以我尝试添加另外两个列表框以测试。该计划现在正在测试里程碑(岁月 - 从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

2 个答案:

答案 0 :(得分: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个条目)

,您应该会看到发生的事情

<强> UPDATE2

我已修改代码,方法是添加Try..Catch块以修复异常,如果文件包含空行/行,其中包含无效数据。

只需替换项目中的代码即可。

另外请确保它是文本文件中使用的“”空格,而不是制表符。如果使用了标签,则替换

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

Dim datePos As Integer = line.LastIndexOf(vbTab)
好运这次好运:)如果它仍然失败,那么我们可以让字符串转换为函数并进行更多测试,甚至通过获取所有3个数字,然后创建一个新的更多手动日期,日,月和年(每个都是整数)

<强> UPDATE3

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

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

更改为从索引+ 1读取,因为1月是索引0,2月索引1等。

答案 1 :(得分: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岁,所以如果点击第一个元素(index = 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

这里是sub :(你看它几乎是1:1,除了退出子,因为没有for-loop可以退出,但是因为该方法包含所有执行的代码直到循环结束,因此退出子与先前版本中的继续相同。

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)