Hi,
EDIT START
As you may have seen I've deleted the example project from this post. I have updated the code slightly so that it compiles with Option Strict On, and I've replaced some MsgBoxResults with DialogResults. I did not want the code I post here to be different from the example project, and since I didn't really see any need for the example project anyway (what's the point if you got all the code?) I just deleted it.
If you need any help implementing the code just ask.
EDIT END
Just created this quickly for someone.
It's a small application with one form with a menubar and a richtextbox. It looks similar to Notepad and behaves exactly the same with the New file, Open file, Save file, Save file As, and Exit functions.
The code is heavily commented so will be easy to understand.
It also keeps track of the Filepath and FileTitle of the opened file.
Most of this can also be used by other programs that don't behave similar to notepad. The 'algorithms' that check wether to just open a new file or wether to save it first (for example) can be used for nearly anything. The Notepad like usage of these functions (Clear RTB text in case of NewFile for example) are in seperate subs.
Here is the full code and attached is the VS2005 project.
Code:Option Strict On Public Class Form1 '// Declare variables Dim DocumentChanged, DocumentSaved As Boolean Dim sFilePath, sFileTitle As String #Region " MENU BAR ITEMS " Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click '// Check if doc has changed If DocumentChanged = True Then '// Doc has changed, ask wether to save old doc or not Dim dialogRes As DialogResult = MessageBox.Show("Document has changed since you last saved it. Do you want to save?", "NotepadExample", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) If dialogRes = DialogResult.Yes Then '// Save '// Check if already saved or "Untitled" If DocumentSaved = True Then '// Already saved before, so save to old path SaveFile() '// Open new file NewFile() Else '// Never saved before, show dialog SaveFileAs() '// Open new file NewFile() End If ElseIf dialogRes = DialogResult.No Then '// Don't save, just open new file NewFile() End If Else '// Doc has not changed so it's safe to open a new file NewFile() End If End Sub Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click '// Check if doc has changed If DocumentChanged = True Then '// Doc has changed, ask wether to save old doc or not Dim dialogRes As DialogResult = MessageBox.Show("Document has changed since you last saved it. Do you want to save?", "NotepadExample", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) If dialogRes = DialogResult.Yes Then '// Save '// Check if already saved or "Untitled" If DocumentSaved = True Then '// Already saved before, so save to old path SaveFile() '// Open file OpenFile() Else '// Never saved before, show dialog SaveFileAs() '// Open file OpenFile() End If ElseIf dialogRes = DialogResult.No Then '// Don't save, just open file OpenFile() End If Else '// Doc has not changed so it's safe to open another file OpenFile() End If End Sub Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click '// Check if saved before If DocumentSaved = True Then '// Saved before, so just save to old path SaveFile() Else '// Never saved before, show dialog SaveFileAs() End If End Sub Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click '// Just save file as SaveFileAs() End Sub Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click '// Close the app '// Saved/Changed checks are in FormClosing event so they also fire when user presses the X Me.Close() End Sub #End Region #Region " NEW/OPEN/SAVE/SAVEALL SUBS " Private Sub NewFile() '// Open a new file '// Clear RTB text and set the filetitle/path RTB.Clear() sFilePath = "" sFileTitle = "Untitled" '// Set saved/changed status DocumentSaved = False DocumentChanged = False '// Update title UpdateTitle() End Sub Private Sub OpenFile() '// Show OpenFileDialog and open selected file Using ofd As New OpenFileDialog '// Set properties ofd.Title = "Open File" ofd.Filter = "Text files (*.txt)|*.txt|All files|*.*" '// Show dialog and only continue when OK was pressed If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then '// Set filepath sFilePath = ofd.FileName '// Get fileTitle via function sFileTitle = IO.Path.GetFileName(sFilePath) '// Load file into RTB RTB.LoadFile(sFilePath, RichTextBoxStreamType.PlainText) '// Set changed/saved status DocumentChanged = False DocumentSaved = True '// Update title UpdateTitle() End If End Using End Sub Private Sub SaveFile() '// Save file to old sFilePath path RTB.SaveFile(sFilePath, RichTextBoxStreamType.PlainText) '// Set changed/saved status DocumentChanged = False DocumentSaved = True '// Update title UpdateTitle() End Sub Private Sub SaveFileAs() '// Show SaveFileDialog and save to selected path Using sfd As New SaveFileDialog '// Set properties sfd.Title = "Save File" sfd.Filter = "Text files (*.txt)|*.txt|All files|*.*" '// Show dialog and only continue when user presses OK If sfd.ShowDialog() = Windows.Forms.DialogResult.OK Then '// Set paths sFilePath = sfd.FileName sFileTitle = IO.Path.GetFileName(sFilePath) '// Save file RTB.SaveFile(sFilePath, RichTextBoxStreamType.PlainText) '// Set status and update title DocumentChanged = False DocumentSaved = True UpdateTitle() End If End Using End Sub #End Region #Region " MISC FUNCTIONS " Private Sub UpdateTitle() '// Set form text to show filetitle Me.Text = sFileTitle & " - [ NotepadExample ]" '// Add a * if doc has changed If DocumentChanged = True Then Me.Text &= "*" End Sub #End Region #Region " FORM AND RTB RELATED " Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing '// Form is closing so check if doc is saved If DocumentChanged = True Then Dim dialogRes As DialogResult = MessageBox.Show("Document has changed since it was last saved. Do you want to save before closing?", "NotepadExample", MessageBoxButtons.YesNoCancel) If dialogRes = DialogResult.Yes Then '// Save If DocumentSaved = True Then SaveFile() Else SaveFileAs() End If ElseIf dialogRes = DialogResult.Cancel Then e.Cancel = True End If End If End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '// Load new file NewFile() End Sub Private Sub RTB_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RTB.TextChanged '// If doc has not changed then set it to changed If DocumentChanged = False Then Me.Text &= "*" DocumentChanged = True End If End Sub #End Region End Class


Reply With Quote


