复制列表框上的项目

时间:2015-04-09 12:38:01

标签: vb.net visual-studio-2012

我有一个程序可以在列表框中记录作业,并按照到期日对其进行排序。但是,当我尝试按日期对数据进行排序时,它会这样做,但会复制列表框中的每个项目。这可以通过关闭程序并再次打开来纠正,但这非常不方便。有什么方法可以解决这个问题吗?

这是两个

的代码

frmSplashScreen:

Imports System.IO
Public Class frmSplashscreen
Dim dates() As Date = {}
Dim temparray() As String = {}
Dim temparray2() As String = {}
Dim Path As String = Application.StartupPath
Dim newuser As Boolean = False
Dim sorted As Boolean = False
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles     btnAdd.Click
    If lstDate.Items.Contains("No assignments yet!") Then
        lstDate.Items.Clear()
    End If
    frmAdd.Show()
    Me.Hide()
End Sub

Private Sub Me_mouseenter(sender As Object, e As EventArgs) Handles Me.MouseEnter
    If sorted = False And lstDate.Items.Contains("No assignments yet!") = False Then
        SortByDate()
    End If
End Sub

Private Sub frmSplashscreen_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    If File.Exists(Path & "Assignments.crw") = False Then
        newuser = True
        lstDate.Items.Add("No assignments yet!")
    Else
        FileOpen(1, Path & "Assignments.crw", OpenMode.Input)
        While Not EOF(1)
            Dim output As String = LineInput(1)
            lstAssignments.Items.Add(Mid(output, InStr(output, "-") + 1, InStr(output, "_") - (InStr(output, "-") + 1)))
            lstAN.Items.Add(Mid(output, InStr(output, "_") + 1, output.Length))
            lstDate.Items.Add(Mid(output, 1, InStr(output, "-") - 1))
            Array.Resize(temparray, temparray.Length + 1)
            temparray(temparray.Length - 1) = output
        End While
    End If
    FileClose(1)
    If lstDate.Items.Contains("No assignments yet!") = False Then
        SortByDate()
    End If
End Sub

Private Sub SortByDate()

    For Each x In lstDate.Items
        MsgBox(x)
        Array.Resize(dates, dates.Length + 1)
        dates(dates.Length - 1) = x
    Next

    Array.Sort(dates)
    ' Sort dates.
    temparray2 = {}
    ' Empty temparray2 for resizing
    Dim counter As Integer = 0
    ' Declare counter
    For Each x In dates
        ' Looping through dates...
        Dim result As Integer
        ' ...Declare result.
        For Each y In temparray
            ' Nestled for loop to check each result against one another. This time looping through temparray.
            result = InStr(y, x)
            ' Search for x (The date) in y (the line of the file)
            If result <> 0 Then
                ' If it is found...
                Array.Resize(temparray2, temparray2.Length + 1)
                counter = counter + 1
                temparray2(counter - 1) = y
                ' Add y to the array temparray2 in chronological order.
            End If
        Next
    Next
    Dim num As Integer = 0
    ' Declare num
    lstAN.Items.Clear()
    lstAssignments.Items.Clear()
    lstDate.Items.Clear()
    temparray = {}
    lstAN.Items.Clear()
    lstAssignments.Items.Clear()
    lstDate.Items.Clear()
    For Each x In temparray2
        Array.Resize(temparray, temparray2.Length)
        temparray(num) = x
        lstAssignments.Items.Add(Mid(x, InStr(x, "-") + 1, InStr(x, "_") - (InStr(x, "-") + 1)))
        lstAN.Items.Add(Mid(x, InStr(x, "_") + 1, x.Length))
        lstDate.Items.Add(Mid(x, 1, InStr(x, "-") - 1))
        num += 1
    Next
    ' Loop through temparray2 making temparray the exact same and adding the array to the listbox.
    sorted = True
End Sub

Private Sub SortMyLife()
    If File.Exists(Path & "Assignments.crw") = False Then
        newuser = True
        lstDate.Items.Add("No assignments yet!")
    Else
        FileOpen(1, Path & "Assignments.crw", OpenMode.Input)
        While Not EOF(1)
            Dim output As String = LineInput(1)
            lstAssignments.Items.Add(Mid(output, InStr(output, "-") + 1, InStr(output, "_") - (InStr(output, "-") + 1)))
            lstAN.Items.Add(Mid(output, InStr(output, "_") + 1, output.Length))
            lstDate.Items.Add(Mid(output, 1, InStr(output, "-") - 1))
            Array.Resize(temparray, temparray.Length + 1)
            temparray(temparray.Length - 1) = output
        End While
    End If
    FileClose(1)
    If lstDate.Items.Contains("No assignments yet!") = False Then
        SortByDate()
    End If
End Sub

Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    If CheckBox1.Checked = True Then
        Call SortMyLife()
        CheckBox1.Checked = False
    End If
End Sub
End Class

frmAdd(用于添加新作业)

Public Class frmAdd
Dim temparray() As String = {}
Dim temparray2() As String = {}
Dim dates() As Date = {}
Dim sorted As Boolean

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    Dim datedue As Date = dtpDue.Text
    Dim Subject As String = cbxSbjct.Text
    Dim Additional As String = rtfAN.Text
    Dim Path As String = Application.StartupPath & "Assignments.crw"
    If datedue > Now() Then
        If Subject <> "Choose a subject" Then
            FileOpen(1, Path, OpenMode.Append)
            Dim output As String = dtpDue.Text & "-" & Subject & "_" & Additional
            If frmSplashscreen.lstAssignments.Items.Contains("No assignments yet!") Then
                frmSplashscreen.lstAssignments.Items.Clear()
                frmSplashscreen.lstDate.Items.Clear()
                frmSplashscreen.lstAN.Items.Clear()
            End If

            frmSplashscreen.lstAssignments.Items.Add(Mid(output, InStr(output, "-") + 1, InStr(output, "_") - (InStr(output, "-") + 1)))
            frmSplashscreen.lstAN.Items.Add(Mid(output, InStr(output, "_") + 1, output.Length))
            frmSplashscreen.lstDate.Items.Add(dtpDue.Text)

            PrintLine(1, output)

            FileClose(1)
            For Each x In frmSplashscreen.lstDate.Items
                MsgBox(x)
            Next
            Me.Close()
            frmSplashscreen.Show()
        Else
            MsgBox("Please enter a subject in the box provided")
        End If
    Else
        MsgBox("Please enter a date in the future", vbInformation, "Error")
    End If
End Sub
End Class

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

在排序发生之前,您是否尝试过像ListBoxHoliday.Items.Clear这样简单的事情?