none
Save form show style

    Question

  • Hi All,

    This code for saving and Read again in Textbox text.

    What I need when Read the form showing like picture 1 not like picture 2.

    picture 1 is.

    picture 2 is;

    code is;

    Imports System
    Imports System.Xml
    Imports System.IO
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
            Dim I As Integer = 0
    
            For I = 0 To 9
                TextBox1.Text &= "Value Text -" & I & vbCrLf
            Next
    
    
        End Sub
    
        Private Sub SaveValue(ByVal Path As String)
    
            Dim ToSave(1) As String
            Dim index As Integer = 0
    
            For Each Control As Control In Me.Controls
                If TypeOf Control Is TextBox Then
                    ToSave(index) = Control.Text
                    index += 1
                    ReDim Preserve ToSave(index)
                End If
            Next
    
            Dim writer As New System.Xml.Serialization.XmlSerializer(GetType(String()))
            Dim file As New System.IO.StreamWriter(Path)
            writer.Serialize(file, ToSave)
            file.Close()
    
        End Sub
    
        Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
            Dim SAVE As New SaveFileDialog
            With SAVE
                .Filter = "xml Files (*.xml)|*.xml|All Files (*.*)|*.*"
                .InitialDirectory = "C:\"
                .Title = "save XML"
            End With
            If SAVE.ShowDialog = Windows.Forms.DialogResult.OK Then
                SaveValue(SAVE.FileName)
            End If
    
        End Sub
    
        Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
            Form2.Show()
        End Sub
    End Class
    
    
    Imports System
    Imports System.Xml
    Imports System.IO
    
    Public Class Form2
    
        Private Sub ReadValue(ByVal Path As String)
    
            Dim reader As New System.Xml.Serialization.XmlSerializer(GetType(String()))
            Dim file As New System.IO.StreamReader(Path)
            Dim ToRead() As String = CType(reader.Deserialize(file), String())
            file.Close()
            Dim index As Integer = 0
    
            For Each Control As Control In Me.Controls
                If TypeOf Control Is TextBox Then
                    Control.Text = ToRead(index)
                    index += 1
                End If
            Next
    
    
        End Sub
    
        Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim save As New OpenFileDialog
            With save
                .Filter = "xml Files (*.xml)|*.xml|All Files (*.*)|*.*"
                .InitialDirectory = "C:\"
                .Title = "SAVE XML"
            End With
            If save.ShowDialog = Windows.Forms.DialogResult.OK Then
                ReadValue(save.FileName)
            End If
        End Sub
    
        Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    End Class

    Sunday, April 30, 2017 7:12 AM

Answers

  • What I need when Read the form showing like picture 1 not like picture 2.

    If the control is a textbox then it has a Lines property.  You can use that property to get each line of text as one element in an array, and use Join to put those lines together with a separator before writing them to the file.  If you use something other than vbCrLf as the separator then, when they are read back, use Split to get them back into an array that you can assign to the Lines property of the textbox.

    Sunday, April 30, 2017 7:59 AM
  • Hi

    As per Acamar, using the Lines property.

    Here is some very basic code to illustrate one way to do it.

    ' Form1 with TextBox1, Button1 and Button2
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim path As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\DummyFile.txt"
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'setup example data and save to path
            ' not needed once file exists
            If Not IO.File.Exists(path) Then
                ' no file, so make one
                TextBox1.Text = Nothing
                TextBox1.AppendText("Value Text - 1" & vbCrLf)
                TextBox1.AppendText("Value Text - 2" & vbCrLf)
                TextBox1.AppendText("Value Text - 3" & vbCrLf)
                TextBox1.AppendText("Value Text - 4" & vbCrLf)
                TextBox1.AppendText("Value Text - 5" & vbCrLf)
                TextBox1.AppendText("Value Text - 6" & vbCrLf)
                TextBox1.AppendText("Value Text - 7" & vbCrLf)
                TextBox1.AppendText("Value Text - 8" & vbCrLf)
                TextBox1.AppendText("Value Text - 9")
                Button1.PerformClick()
            End If
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ' save textbox contents 
            Using fn As IO.StreamWriter = New IO.StreamWriter(path, False)
                For Each s As String In TextBox1.Lines
                    fn.WriteLine(s)
                Next
            End Using
            MessageBox.Show("Saved Textbox text")
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            ' reload textbox contents
            Dim line As String = Nothing
            If IO.File.Exists(path) Then
                ' first, clear current textbox contents
                TextBox1.Clear()
                Using sr As IO.StreamReader = New IO.StreamReader(path)
                    Do
                        line = sr.ReadLine()
                        If Not line = Nothing Then
                            TextBox1.AppendText(line & vbCrLf)
                        End If
                    Loop Until line = Nothing
                End Using
                MessageBox.Show("TextBox text reloaded")
            Else
                MessageBox.Show("File not present - nothing to reload")
            End If
        End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Marked as answer by mipakteh Sunday, April 30, 2017 3:39 PM
    • Edited by leshay Friday, May 05, 2017 11:33 PM
    Sunday, April 30, 2017 3:28 PM
  • I don't know how to write because we have 3 string

    The only one you need to be concerned with is the one that is written to or read from the file.

    Change the write routine to join the lines together:

            For Each Control As Control In Me.Controls
                If TypeOf Control Is TextBox Then
                    Dim TB As TextBox = CType(Control, TextBox)
                    ToSave(index) = String.Join("~", TB.Lines)
                    index += 1
                    ReDim Preserve ToSave(index)
                End If
            Next
    and change the read routine to split them apart:
            For Each Control As Control In Me.Controls
                If TypeOf Control Is TextBox Then
                    Dim TB As TextBox = CType(Control, TextBox)
                    TB.Lines = Split(ToRead(index), "~")
                    index += 1
                End If
            Next


    • Marked as answer by mipakteh Monday, May 01, 2017 2:25 AM
    Sunday, April 30, 2017 9:57 PM

All replies

  • What I need when Read the form showing like picture 1 not like picture 2.

    If the control is a textbox then it has a Lines property.  You can use that property to get each line of text as one element in an array, and use Join to put those lines together with a separator before writing them to the file.  If you use something other than vbCrLf as the separator then, when they are read back, use Split to get them back into an array that you can assign to the Lines property of the textbox.

    Sunday, April 30, 2017 7:59 AM
  • I don't know how to write because we have 3 string

    1.File

    2.reader

    3.ToRead

    which one want to split?

    Please show me.

    Sunday, April 30, 2017 2:41 PM
  • Hi

    As per Acamar, using the Lines property.

    Here is some very basic code to illustrate one way to do it.

    ' Form1 with TextBox1, Button1 and Button2
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim path As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\DummyFile.txt"
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'setup example data and save to path
            ' not needed once file exists
            If Not IO.File.Exists(path) Then
                ' no file, so make one
                TextBox1.Text = Nothing
                TextBox1.AppendText("Value Text - 1" & vbCrLf)
                TextBox1.AppendText("Value Text - 2" & vbCrLf)
                TextBox1.AppendText("Value Text - 3" & vbCrLf)
                TextBox1.AppendText("Value Text - 4" & vbCrLf)
                TextBox1.AppendText("Value Text - 5" & vbCrLf)
                TextBox1.AppendText("Value Text - 6" & vbCrLf)
                TextBox1.AppendText("Value Text - 7" & vbCrLf)
                TextBox1.AppendText("Value Text - 8" & vbCrLf)
                TextBox1.AppendText("Value Text - 9")
                Button1.PerformClick()
            End If
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ' save textbox contents 
            Using fn As IO.StreamWriter = New IO.StreamWriter(path, False)
                For Each s As String In TextBox1.Lines
                    fn.WriteLine(s)
                Next
            End Using
            MessageBox.Show("Saved Textbox text")
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            ' reload textbox contents
            Dim line As String = Nothing
            If IO.File.Exists(path) Then
                ' first, clear current textbox contents
                TextBox1.Clear()
                Using sr As IO.StreamReader = New IO.StreamReader(path)
                    Do
                        line = sr.ReadLine()
                        If Not line = Nothing Then
                            TextBox1.AppendText(line & vbCrLf)
                        End If
                    Loop Until line = Nothing
                End Using
                MessageBox.Show("TextBox text reloaded")
            Else
                MessageBox.Show("File not present - nothing to reload")
            End If
        End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Marked as answer by mipakteh Sunday, April 30, 2017 3:39 PM
    • Edited by leshay Friday, May 05, 2017 11:33 PM
    Sunday, April 30, 2017 3:28 PM
  • I don't know how to write because we have 3 string

    The only one you need to be concerned with is the one that is written to or read from the file.

    Change the write routine to join the lines together:

            For Each Control As Control In Me.Controls
                If TypeOf Control Is TextBox Then
                    Dim TB As TextBox = CType(Control, TextBox)
                    ToSave(index) = String.Join("~", TB.Lines)
                    index += 1
                    ReDim Preserve ToSave(index)
                End If
            Next
    and change the read routine to split them apart:
            For Each Control As Control In Me.Controls
                If TypeOf Control Is TextBox Then
                    Dim TB As TextBox = CType(Control, TextBox)
                    TB.Lines = Split(ToRead(index), "~")
                    index += 1
                End If
            Next


    • Marked as answer by mipakteh Monday, May 01, 2017 2:25 AM
    Sunday, April 30, 2017 9:57 PM
  • Thank you very much Acamar.
    Monday, May 01, 2017 2:25 AM