I've created a Text Editor for VB.NET using the code below, however, I'm having some issues with it.
- If I open a file and make changes to it, then save it, it overwrites the original file. If I then go to Open file and open the file I get blank text but if I repeat the process it opens OK.
- Also when I exit, I want to be prompted to save the file ONLY if changes have been made but I get prompted everytime, even if I'm trying to close a blank file
Any help would be appreciated.
Public Class Form1
Dim ChangesMade As Boolean = False ' Used to check if file changed
' Open function
Private Sub open()
Dim Open As New OpenFileDialog() 'Creates a new Open file dialog
Dim myStreamReader As System.IO.StreamReader
Open.Filter = "Text Files (*.txt)|*.txt|All files (*.*)|*.*" ' File Filter
Open.CheckFileExists = True ' Check if file exists
Open.Title = "Open File" ' Sets the dialog title
Open.ShowDialog(Me) ' Opens the open file dialog
Try
Open.OpenFile()
myStreamReader = System.IO.File.OpenText(Open.FileName)
document.Text = myStreamReader.ReadToEnd()
Catch ex As Exception
' Do nothing on Exception
End Try
End Sub
' Save function
Private Sub save()
Dim Save As New SaveFileDialog() 'Creates a new Save file dialog
Dim myStreamWriter As System.IO.StreamWriter
Save.Filter = "Text Files (*.txt)|*.txt|All files (*.*)|*.*" ' File Filter
Save.CheckFileExists = False ' Allows new file to be created
Save.Title = "Save File" ' Sets the dialog title
Save.ShowDialog(Me) ' Opens the save file dialog
Try
myStreamWriter = System.IO.File.CreateText(Save.FileName)
myStreamWriter.Write(document.Text)
myStreamWriter.Flush()
Catch ex As Exception
' Do nothing on Exception
End Try
End Sub
Private Sub document_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles document.TextChanged
ChangesMade = True
End Sub
' Changes function
Private Sub changes()
If ChangesMade Then
If MessageBox.Show("Do you want to save?", "Save Changes", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
save() ' If user clicks 'Yes' save file dialog appears
End If
ChangesMade = False
End If
End Sub
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
changes() ' Call changes function
open() ' Call open function
End Sub
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
save() ' Call save function
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click 'Onclick Exit menu, bring up a message box and ask if they want to save.
changes() ' Call changes function
End
End Sub
Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click
document.Cut() ' Cut text
End Sub
Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
document.Copy() ' Copy text
End Sub
Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
document.Paste() ' Paste text
End Sub
End Class