Public Class Form1
'set the savedoc variable value as true
Dim savedoc As Boolean = True
Private Sub menuOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuOpen.Click
'declare open as new openFiledialog so that when the Open Sub menu is click,
'the OpenfileDialog will appear.
Dim Open As New OpenFileDialog()
'it is declare as System input and output Streamreader
'it reads characters from byte stream in a particular encoding
Dim myStreamReader As System.IO.StreamReader
'in an open dialog box, it will give an opening filter for the current filenames,
'or the save file types.
Open.Filter = "Text [*.txt*]|*.txt|All Files [*.*]|*.*"
'it checks if the file is exist or not
Open.CheckFileExists = True
'sets the openfile dialog name as "OpenFile"
Open.Title = "OpenFile"
Open.ShowDialog(Me)
Try
'it opens the selected file by the user
Open.OpenFile()
'opens the existing file
myStreamReader = System.IO.File.OpenText(Open.FileName)
'it reads the streams from current position to the end of position and display the result to RichTextBox as Text
RichTextBox1.Text = myStreamReader.ReadToEnd()
Catch ex As Exception
'it will catch if any errors occurs
MsgBox(ex
.Message, MsgBoxStyle
.Information) End Try
End Sub
Private Sub menuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuSave.Click
'call the savefiledialog
SaveFileDialog1.ShowDialog()
'we use StreamWriter for writing characters to a stream
Dim myStreamWriter As System.IO.StreamWriter
'we set default for savefiledialog filter
SaveFileDialog1.Filter = "Text [*.txt*]|*.txt|All Files [*.*]|*.*"
'set the checkpath to true
SaveFileDialog1.CheckPathExists = True
'set the Savefiledialog title
SaveFileDialog1.Title = "Save File"
Try
'gets a string containing the filename selected in the dialog box
myStreamWriter = System.IO.File.AppendText(SaveFileDialog1.FileName)
'it writes the string to stream
myStreamWriter.Write(RichTextBox1.Text)
'clears all buffers for the current writer and causes any
'buffered data to be written to the underlying stream
myStreamWriter.Flush()
Catch ex As Exception
'catch unnecesary errors
End Try
'shows we have saved the most recent changes
savedoc = True
End Sub
Private Sub menuSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuSaveAs.Click
'show the savefiledialog
SaveFileDialog1.ShowDialog()
'if the user encode the file and click save
If SaveFileDialog1.ShowDialog = DialogResult.OK Then
'it create a streamwriter to write in to the file
Dim writer As New System.IO.StreamWriter(SaveFileDialog1.FileName, False)
'get the content of Richtextbox and write to a stream
writer.Write(RichTextBox1.Text)
'close the writer
'it gets the data contains by the control and save the filename for later use.
RichTextBox1.Tag = SaveFileDialog1.FileName
End If
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim Result As DialogResult
'if the current unsaved changes occur we will have to use this option
If RichTextBox1.Text <> "" Then
If savedoc = True Then
Result = MessageBox.Show("Would you like to save changes?", "Confirm Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If Result = Windows.Forms.DialogResult.Yes Then
SaveFileDialog1.ShowDialog()
Dim myStreamWriter As System.IO.StreamWriter
SaveFileDialog1.Filter = "Text [*.txt*]|*.txt|All Files [*.*]|*.*"
SaveFileDialog1.CheckPathExists = True
SaveFileDialog1.Title = "Save File"
SaveFileDialog1.ShowDialog(Me)
Try
myStreamWriter = System.IO.File.AppendText(SaveFileDialog1.FileName)
myStreamWriter.Write(RichTextBox1.Text)
myStreamWriter.Flush()
Catch ex As Exception
MessageBox.Show("There has been an error in the saving process")
End Try
savedoc = True
Application.Exit()
End If
If Result = Windows.Forms.DialogResult.No Then
Application.Exit()
End If
Else : Application.Exit()
End If
Else : Application.Exit()
End If
End Sub
End Class