Read & Write different lines of text to and from a text file


Imports System 			' < Contains the basic objects of .Net like String, Char, Integer, etc
Imports System.IO 		' < Contains the StreamReader and StreamWriter objects amongst others
Imports System.Windows.Forms 	' < Contains SaveFileDialog & OpenFileDialog objects amongst others

Module test
    Dim cFileName As String = ""
    Dim dlgSaveFile As New SaveFileDialog
    Dim dlgOpenFile As New OpenFileDialog
    Dim TextBox1 as New TextBox
    Dim TextBox2 As New TextBox
    Dim btnLoadText As New Button
    Dim btnSaveText As New Button

    Public Sub Main
        ' Create new Form
        Dim f As New Form
        ' Set textbox1 values
        With TextBox1
            .Name = "TextBox1"
            .Top = 10
            .Left = 10
        End With
        ' Add TextBox1 to form's control collection
        f.Controls.Add(TextBox1)
        ' Set textbox2 values
        With TextBox2
            .Name = "TextBox2"
            .Top = TextBox1.Top + TextBox1.Height + 5
            .Left = 10
        End With
        ' Add TextBox1 to form's control collection
        f.Controls.Add(TextBox2)
        ' Set btnLoadText Values
        With btnLoadText
            .Name = "btnLoadText"
            .Text = "Load"
            .Top = TextBox1.Top
            .Left = TextBox1.Left + textBox1.Width + 5
        End With
        ' Add btnLoadText to form's control collection
        f.Controls.Add(btnLoadText)
        ' Set btnSaveText Values
        With btnSaveText
            .Text = "Save"
            .Top = TextBox2.Top
            .Left = TextBox2.Left + textBox2.Width + 5
        End With
        ' Add btnLoadText2 to form's control collection
        f.Controls.Add(btnSaveText)
        ' Replace btnLoadText.Click event with a local routine
        AddHandler btnLoadText.Click, AddressOf mybtnLoadTextClick
        ' Replace btnSaveText.Click event with a local routine
        AddHandler btnSaveText.Click, AddressOf mybtnSaveTextClick
        ' Show the Form
        f.showdialog
    End Sub
    
    Public Sub mybtnLoadTextClick(ByVal sender As Object, ByVal e As EventArgs)
        ' When clicking on btnLoadText, LoadText is executed
        LoadText
    End Sub
    
    Public Sub mybtnSaveTextClick(ByVal sender As Object, ByVal e As EventArgs)
        ' When clicking on btnSaveText, SAaveText is executed
        SaveText
    End Sub
    
    Public Sub SaveText()
         'Set default file extention, in this case save as .txt file
         dlgSaveFile.Filter = "txt files (*.txt)|*.txt"
         'Let user select a file
         If dlgSaveFile.ShowDialog() = DialogResult.OK Then
            'User has clicked OK/selected a file
            cFileName= dlgSaveFile.FileName
            'Creates/overwrites cFilename
            Dim sw as New StreamWriter(cFileName)
            'Write text to file
            sw.WriteLine(TextBox1.Text)
            sw.WriteLine(TextBox2.Text)
            'close file
            sw.Close
        End If
    End Sub
    
    Public Sub LoadText()
        'Set default file extention, in this case open *.txt only
         dlgOpenFile.Filter = "txt files (*.txt)|*.txt"
        'Let User select a file
        If dlgOpenFile.ShowDialog = DialogResult.OK Then
            'User clicked OK/selected output location
            cFileName = dlgOpenFile.FileName
            'Open file
            Dim sr As New StreamReader(cFileName)
            'Read text into fields
            TextBox1.Text = sr.ReadLine
            TextBox2.Text = sr.ReadLine
            'close file
            sr.Close
        End If
    End Sub
    
End Module