-
Feb 28th, 2008, 04:42 AM
#1
[2005] Notepad-like New/Open/Save/SaveAs/Exit behaviour
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
Last edited by NickThissen; Aug 30th, 2009 at 11:58 AM.
-
Feb 28th, 2008, 06:24 AM
#2
Re: [2005] Notepad-like New/Open/Save/SaveAs/Exit behaviour
If you have access to Winzip, a zip file would be helpful as that is what most of our members use.
Thanks.
-
Feb 28th, 2008, 02:29 PM
#3
Re: [2005] Notepad-like New/Open/Save/SaveAs/Exit behaviour
Alright I attached the zip file aswell in the first post. Thanks.
-
Jan 5th, 2009, 05:29 AM
#4
Addicted Member
Re: [2005] Notepad-like New/Open/Save/SaveAs/Exit behaviour
-
Feb 15th, 2010, 05:50 PM
#5
Junior Member
Re: [2005] Notepad-like New/Open/Save/SaveAs/Exit behaviour
hi.. i have problems with this:
Code:
If DocumentSaved = True Then
SaveFile()
Else
SaveFileAs()
End If
ElseIf dialogRes = DialogResult.Cancel Then
e.Cancel = True
e.Cancel is not a member of System.EventArgs it says.. so idk what to replace it with.. i use vs 2008
SaveFile() needs to be declared it says.. but as what? Thanks
I wanna see you plead and scream, and cry to God..
-
Feb 15th, 2010, 06:19 PM
#6
Re: [2005] Notepad-like New/Open/Save/SaveAs/Exit behaviour
From where are you calling/executing this code?
-
Feb 15th, 2010, 06:33 PM
#7
Junior Member
Re: [2005] Notepad-like New/Open/Save/SaveAs/Exit behaviour
e.Cancel issue solved.. forgot the edit the executing thing.. but this still gives me errores
Code:
If DocumentSaved = True Then
SaveFile()
Else
SaveFileAs()
End If
SaveFile() and SaveFileAs() needs to be declared.. =/
I wanna see you plead and scream, and cry to God..
-
Feb 15th, 2010, 07:57 PM
#8
Re: [2005] Notepad-like New/Open/Save/SaveAs/Exit behaviour
I would expect that would give you errors unless you have written subs/functions called "SaveFile" or "SaveFileAs"
Post the code that you have written for these...
-
Feb 15th, 2010, 08:49 PM
#9
Junior Member
Re: [2005] Notepad-like New/Open/Save/SaveAs/Exit behaviour
Originally Posted by Hack
I would expect that would give you errors unless you have written subs/functions called "SaveFile" or "SaveFileAs"
Post the code that you have written for these...
Code:
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
Dim Save As New SaveFileDialog()
Dim myStreamWriter As System.IO.StreamWriter
Save.Filter = "Text [*.txt*]|*.txt|All Files [*.*]|*.*"
Save.CheckPathExists = True
Save.Title = "Save File"
Save.ShowDialog(Me)
Try
myStreamWriter = System.IO.File.AppendText(Save.FileName)
myStreamWriter.Write(RichTextBox1.Text)
myStreamWriter.Flush()
Catch ex As Exception
End Try
End Sub
i only have Save not save as
e
edit: ok i guess i will add Save As.. the code i have now is the one above.. but i will change it to the one in this tutorial since i think it's more reliable
Last edited by Stealth1337; Feb 15th, 2010 at 11:04 PM.
I wanna see you plead and scream, and cry to God..
-
Feb 16th, 2010, 11:02 AM
#10
Re: [2005] Notepad-like New/Open/Save/SaveAs/Exit behaviour
If you look in my code, there's a method SaveFile and a method SaveFileAs. You'd need to have those methods too before you can call them.
This is meant as a complete example. Of course you can use bits and pieces from it, but you'll need to figure out for yourself which bits you need to change/replace/remove/add, because you won't be able to simply take a chunk out of the code and expect it to work.
-
Feb 16th, 2010, 07:06 PM
#11
Junior Member
Re: [2005] Notepad-like New/Open/Save/SaveAs/Exit behaviour
Originally Posted by NickThissen
If you look in my code, there's a method SaveFile and a method SaveFileAs. You'd need to have those methods too before you can call them.
This is meant as a complete example. Of course you can use bits and pieces from it, but you'll need to figure out for yourself which bits you need to change/replace/remove/add, because you won't be able to simply take a chunk out of the code and expect it to work.
i guess you are speaking of this:
Code:
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 "
i'll try to do my best to figure this out.. thank u for this great help
I wanna see you plead and scream, and cry to God..
-
Feb 16th, 2010, 09:11 PM
#12
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|