2012-08-12 43 views
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 
+1

你從哪裏得到itemcount的價值? – Steve 2012-08-12 14:40:07

+1

是被拋出的異常嗎?你的代碼默默地忽略任何異常。 – 2012-08-12 14:42:01

+0

是的,正如@orzechowskid所說,刪除那個空的catch,看看Index超出範圍的異常是否出現 – Steve 2012-08-12 14:48:39

回答

0

您需要Set a break point就行For loops = 1 To itemcount - 1

然後按F8鍵,併爲您的代碼執行檢查的itemcount值。使用這篇關於debugging in vb.net的文章來幫助你。

從評論中聽起來好像itemcount的值在某些方面不正確。