保存按钮不保存所有内容

时间:2012-08-12 14:35:50

标签: vb.net

我的项目中有一个保存按钮,它应该将我的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

1 个答案:

答案 0 :(得分:0)

您需要For loops = 1 To itemcount - 1

Set a break point

然后按F8键并在代码执行时检查itemcount的值。使用有关debugging in vb.net的文章来帮助您。

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