Derek Jeder
Derek Jeder

Reputation: 79

How to Save, Separate, and Write Data from File to Multiple TextBoxes

I need to save a bunch (around 600) of TextBox content to a (Preferably) single file, then be able to write that data back to their respective TextBoxes on file open. Essentially I'm making a D&D stat sheet, and I want to save what each player has entered as the [character's name].txt. I already got simple saving and writing down by using a single control and the code:

Dim SaveCharacter As StreamWriter
        If txtName.Text = "" Then
            MessageBox.Show("In order to save, you must enter a name for your character.", "Error: No Name Entered")
        Else
            Try
                SaveCharacter = File.AppendText("H:\Visual Basic Projects\DAndD-2.5\DAndD-2.5\Saves\" & txtName.Text & ".txt")
                SaveCharacter.WriteLine(frmNotes.rtxNotes.Text)
                SaveCharacter.Close()
            Catch
                MessageBox.Show("Error")
            End Try
        End If

 'Next event handler
            Dim OpenCharacter As StreamReader
            OpenCharacter = File.OpenText("H:\Visual Basic Projects\DAndD-2.5\DAndD-2.5\Saves\Jared.txt")
            Do Until OpenCharacter.EndOfStream
                frmNotes.rtxNotes.Text = OpenCharacter.ReadToEnd.ToString()
                For Each strLine As String In frmNotes.rtxNotes.Text.Split(vbNewLine)
                Next
            Loop

Is there an easy(ish) way to do what I need, especially in mass (few lines of code) without having to save each TextBox as a different .txt file?

Thanks for the help!

Upvotes: 0

Views: 840

Answers (2)

SSS
SSS

Reputation: 5393

Loop through all the controls and pull out the TextBoxes:

  Sub SaveFile(strFilename As String)
    Using fs As New System.IO.FileStream(strFilename, IO.FileMode.OpenOrCreate)
      Using sr As New System.IO.StreamWriter(fs)
        For Each ctl As Control In Me.Controls
          If TypeOf ctl Is TextBox Then
            Dim txt As TextBox = DirectCast(ctl, TextBox)
            sr.WriteLine(txt.Name & ":" & txt.Text)
          End If
        Next ctl
      End Using
    End Using
  End Sub

  Sub Readfile(strfilename As String)
    Using fs As New System.IO.FileStream(strfilename, IO.FileMode.Open)
      Using sr As New System.IO.StreamReader(fs)
        Do Until sr.EndOfStream
          Dim strLine As String = sr.ReadLine
          Dim colonposition As Integer = strLine.IndexOf(":")
          If colonposition > 0 And colonposition < strLine.Length - 1 Then
            Dim strTextBoxName As String = strLine.Substring(0, colonposition)
            Dim strTextBoxText As String = strLine.Substring(colonposition + 1)
            Dim ctl() As Control = Me.Controls.Find(strTextBoxName, False)
            If ctl.Length > 0 AndAlso TypeOf ctl(0) Is TextBox Then
              DirectCast(ctl(0), TextBox).Text = strTextBoxText
            End If
          End If
        Loop
      End Using
    End Using
  End Sub

The above code only works for textboxes on the form itself. You can rework it to be a recursive function to walk the containers, but I'll leave that as an exercise for the reader.

Upvotes: 3

jmcilhinney
jmcilhinney

Reputation: 54417

This will write the contents of all your TextBoxes to a file, one line per control:

IO.File.WriteAllLines(filePath, Me.Controls.OfType(Of TextBox).Select(Function(tb) tb.Text))

This will read a file back into those TextBoxes:

Dim textBoxes = Me.Controls.OfType(Of TextBox).ToArray()
Dim lines = IO.File.ReadAllLines(filePath)

For i = 0 To lines.GetUpperBound(0)
    textBoxes(i).Text = lines(i)
Next

That assumes that no TextBoxes are multi-line and it also does not validate the file on reading, so you might want to check that the number of lines and TextBoxes matches.

Upvotes: 1

Related Questions