基本的に記入するフォームである多数のテキストボックスを備えたアプリがあります。タブコントロールにはいくつかのタブページがあります。データを保存する際には、テキストボックスをループして名前とテキストを取得し、それを「|」付きのテキストファイルに入れます。セパレーターとして。これはうまく機能し、すべてのテキストボックスとテキストファイル内のデータを確認できます。ここで問題が発生します。ファイルを読み戻すと(保存されたデータをフォームにロードして戻すため)、コントロール名をループして、テキストをファイル内のテキストに変更します。フォーム上にあるテキストボックスでは正常に機能しますが、タブページ上にある最初のテキストボックスに到達すると失敗します。どうすれば修正できますか?そして、データを保存するためのより良い方法があれば、私はすべての耳です。
ファイルを書き込むコードは次のとおりです。
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
それで十分な情報だといいのですが。助けてくれてありがとう!