This tutorial is here for you to learn how to make these functions: Open, Save, New and modified file


Step 1
All you'll need a is a richtextbox

Now we'll start with the basic functions. Open, Saving, Save As, Closing and New Pages

Write one for each of these functions in bold and add 2 strings
Code:
Dim FileName, SafeFileName As String

Private sub NewPage

End Sub

Step 2
Before we start coding these, we'll need something to check if the file was modified.
This method may be a little confusing so i'll explain the best way I can.
Add:
Code:
 Private Sub Check()

End Sub

Private Function Dirty()

End Function

Dim Result as integer 'Keep this out of a sub
With this function, Dirty, this will be used to return whether richtextbox was modified or not. With dirty producing a value, sub Check will display a message box, saying if you wanna save or not or cancel and Result will set an integer. This method will be used when the form is closing, opening and making a new page.

To do this:
Code:
Private Function Dirty()
        If MyRichTextBox.Modified = False Then
            Return False ' Nothing have been changed
        Else
            Return True ' Changes has been made
        End If
    End Function

Private Sub Check()
        If IsDirty() = True Then
            Dim msg As Integer
            msg = MessageBox.Show("Changes have been made. Would you like to save?", "Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information)
            If msg = MsgBoxResult.Yes Then
                result = 1
            ElseIf msg = MsgBoxResult.No Then
                result = 2
            ElseIf MsgBoxResult.Cancel Then
                result = 3
            End If
        Else
            result = 2
        End If
    End Sub
As said before, Result will be given a number for the corresponding responses. If yes is pressed, the number 1 will equal Result.
You can also think of it as a string. If yes is pressed then Result will equal, "to be saved". I'm using integers to be more precise with the coding.

Remember .. This part here is only for when the form is closing, opening and making a new page.
Now, all we need to do is add this our events that is bold. Form.Closing, Sub Open and Sub NewPage

To do that, just add Check()
Why? Check is what using the function Dirty and using the value it produces to know if it should ask you save or not.
There is one more thing to add before Step 2 is over. What happens if result equals 1, 2 or 3
Thats easy

If the form is closing,
Check()
Result 1 = Save()
Result 2 = Nothing, Let it continue what it suppose to do
Result 3 = e.Cancel = true

Now Open and NewPage will be quite more complicated since it has to be perfect with each case. Such as, Opening, there multiple file types to open.

New Page
Code:
Private Sub NewPage()       
 Check()
        If Result = 1 Then ' Saves the file
            Save()
             RichTextBox.Clear()
             RichTextBox.ClearUndo()
             RichTextBox.Modified = False
            FileName = Nothing
            SafeFileName = Nothing
        End If
        If Result = 2 Then 'Doesn't save, but continues NewPage functions
             RichTextBox.Clear()
            RichTextBox.ClearUndo()
            RichTextBox.Modified = False
            FileName = Nothing
            SafeFileName = Nothing
        End If
        If Result = 3 Then ' Cancels
            Exit Sub
        End If
    End Sub
Opening (.txt and .rtf files)
Since in this tutorial I am basing things off a richtextbox. When opening .rtf files, it'll load correctly since richtextbox and .rtf (Rich Text File) are compatible. This is not the case with .txt (Text File). Instead of using richtextbox load event, we'll use a stream reader and writer instead.
Like before, we'll need to use our sub Check() then a openfiledialog. You can make one using the toolbox or add one using code. I'll use code
Code:
Check()      
  Dim dlg As OpenFileDialog = New OpenFileDialog ' Makes a new openfile dialog named dlg
        dlg.Title = "Save" : dlg.Filter = "Text files |*.txt|Richtext files |*.rtf|All files |*.*" : dlg.RestoreDirectory = True ' dlg Properties 
        If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then ' When dlg button Ok is pressed
' Here is where we'll add that magic :)
        End If
Now we'll need to set our FileName, SafeFileName and a code that'll open .rtf and .txt files
FileName's and SafeFileName's can be acquired through the dialog itself.
FileName = dlg.FileName
SafeFileName = dlg.SafeFileName

Now we'll need to separate those 2 files. How can we do this? Well, .txt and .rtf are 2 different file types so we'll get separate them using their extension.
To do that .. just declare a string
Dim ext as string = Path.GetExtention(FileName) (You'll have to include system.I-O or just type that in front of Path)
Now we have a string that'll tell us if the file type is a .txt or a .rtf
Code:
ext = ext.ToUpper()
            Select Case ext
                Case ".RTF"
                    MyRichTextBox.LoadFile(FileName) 
                Case ".TXT"
                    Dim Read As System****.StreamReader
                    Read = New System****.StreamReader(FileName)
                     RichTextBox.Text = txtReader.ReadToEnd
                    Read.Close()
                    Read = Nothing
                Case Else
                    MsgBox("File type is not supported") ' Opened a non .rtf or .txt file
            End Select

Step 3
No we have 2 more subs to fill in. Save and Save As

These are are like open but instead of reading and opening, its writing and saving.

The difference between save and save as
Save just saves an opened file
Save As saves as a new file

For Save()
You'll just need to save the file to the string you have called filename
Code:
Private Sub Save()
        If FileName = Nothing Then ' If there is no opened file
            SaveAs() ' SaveAs is used instead
        Else
            If FileName.Contains(".txt") Then
                Dim Writer As System****.StreamWriter
                Writer = New System****.StreamWriter(FileName)
                Writer.Write(MyRichTextBox.Text)
                Writer.Close()
                Writer= Nothing
                RichTextBox.Modified = False
            ElseIf FileName.Contains(".rtf") Then
                RichTextBox.SaveFile(FileName)
            End If
        End If
    End Sub
For SaveAs()
Code:
Private Sub SaveAs()
        Dim dlg As SaveFileDialog = New SaveFileDialog
        dlg.Title = "Save" : dlg.Filter = "Text files |*.txt|Richtext files |*.rtf" : dlg.RestoreDirectory = True
        If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then


            FileName = dlg.FileName
            SafeFileName = System****.Path.GetFileName(FileName)
            Dim Ext As String
            Ext = System****.Path.GetExtension(FileName)
            Ext = Ext.ToUpper()

Select Case Ext
                Case ".RTF"
                    RichTextBox.SaveFile(FileName)
                Case Else
                    Dim tWriter As System****.StreamWriter
                    Writer = New System****.StreamWriter(FileName)
                    Writer.Write(MyRichTextBox.Text)
                    Writer.Close()
                    Writer = Nothing
                    RichTextBox.Modified = False
            End Select
        End If
    End Sub
and thats it!