0
我有一個應用程序,其中有很多文本框,基本上是一個表格來填寫。在選項卡控件中有幾個標籤頁。在保存數據時,我只需循環遍歷文本框,獲取它們的名稱和文本,並將其放入帶有'|'的文本文件中作爲分離者。這很好,我可以看到我的所有文本框和文本文件中的數據。現在出現了這個問題。當我讀取文件(將保存的數據加載回表單)時,它循環遍歷控件名稱,並將文本更改爲文件中的任何內容。它適用於表單上的文本框,但在到達頁面上的第一個文本框時會失敗。我該如何解決這個問題?如果有更好的方法來保存數據,我全都聽過。更新文本框中的textbox.text
下面是寫入文件的代碼:
Private Sub savefile(file As String)
Dim ctl As Control = Me
Dim sw As System.IO.StreamWriter
sw = My.Computer.FileSystem.OpenTextFileWriter(file, False)
Do
ctl = Me.GetNextControl(ctl, True)
If ctl IsNot Nothing Then
If TypeOf ctl Is TextBox Then
sw.WriteLine(ctl.Name.ToString.Substring(3) & "|" & ctl.Text)
End If
End If
Loop Until ctl Is Nothing
sw.Close()
End Sub
下面是讀取文件並更新文本框代碼:
Private Sub ReadFile(filename)
Dim strfilename As String = filename
Dim num_rows, x, y As Integer
Dim strarray(1, 1) As String
Dim ctrl As Control = Me
'Check if file exist
If File.Exists(strfilename) Then
Dim tmpstream As StreamReader = File.OpenText(strfilename)
Dim strrecords() As String
Dim strfields() As String
'Load content of file to strLines array
strrecords = tmpstream.ReadToEnd().Split(vbCrLf)
' Redimension the array.
num_rows = UBound(strrecords)
ReDim strarray(num_rows, 2)
' Copy the data into the array.
For x = 0 To num_rows - 1
strfields = strrecords(x).Split("|")
For y = 0 To 1
strarray(x, y) = strfields(y)
Next
Next
' Display the data in listbox
For x = 0 To num_rows - 1
Dim tbxname As String = strarray(x, 0)
tbxname = Remove(tbxname) 'routine that removes invisible characters
tbxname = "tbx" & tbxname
MsgBox(tbxname) 'used to verify each file and which one fails
Me.Controls(tbxname).Text = strarray(x, 1)
Next
Else : MsgBox("File doesn't exist!")
End If
End Sub
我希望這是足夠的信息。提前感謝任何幫助!