0

我的项目中有一个保存按钮,它应该将我的 2 个列表框的内容保存到一个文本文件中,但它并没有保存所有内容。相反,它会删除其中一个列表框的最后 5 行。我的代码做错了什么?

Dim loops As Integer 'Declare variable
    Dim savefile As New SaveFileDialog
    savefile.FileName = ""
    savefile.Filter = "textfiles(*.txt)|*.txt|file(*)|*|All files('.')|'.')"
    savefile.Title = "save"
    savefile.ShowDialog()
    Try
        Dim write As New System.IO.StreamWriter(savefile.FileName) 'Write and save a new file
        For loops = 1 To itemcount - 1 'loop until no lines are left in listbox
            write.WriteLine(firstname(loops)) 'Write out firstname
            write.WriteLine(lastname(loops)) 'Write out lastname
            write.WriteLine(gender(loops)) 'Write out gender 
            write.WriteLine(applicationdate(loops)) 'Write out date of regestration
            write.WriteLine(address(loops)) 'Write out address
        Next
        write.Close() 'Close file
        MsgBox("File Saved") 'Display message box
    Catch ex As Exception
    End Try
4

1 回答 1

0

你需要在行上设置一个断点For loops = 1 To itemcount - 1

然后按 F8 键并在itemcount代码执行时检查 的值。使用这篇关于在 vb.net 中调试的文章来帮助您。

从评论中听起来好像 的值itemcount在某种程度上是不正确的。

于 2012-08-13T11:05:44.777 回答