|
-
Apr 22nd, 2007, 10:37 AM
#1
Thread Starter
Banned
[RESOLVED]save a file (not save as) SAVE
hey one more problem how do I save the file
The save as works ok but for save if the file has not been saved before the savefiledialog should be called and if any changes were made then the changes should be saved How do I do that?
Last edited by ethicalhacker; Apr 23rd, 2007 at 08:09 AM.
-
Apr 22nd, 2007, 10:44 AM
#2
Re: save a file (not save as) SAVE
What file are you talking about??
Are you saving what's in a RichTextBox or something else?
-
Apr 22nd, 2007, 10:51 AM
#3
Thread Starter
Banned
Re: save a file (not save as) SAVE
 Originally Posted by stimbo
What file are you talking about??
Are you saving what's in a RichTextBox or something else?
Yes yes Im making an mdi text editor so the contents of the rtb get saved as a file using rtb.savefile()
-
Apr 22nd, 2007, 10:58 AM
#4
Re: save a file (not save as) SAVE
Without trying this out I would imagine you would want to declare a class level variable which indicates if the file has been saved once (i.e. given a path and name - which you would also want to store).
vb Code:
Private hasBeenSaved As Boolean = False
Private rtbFileName As String
If the person goes to save the file the first time then check your variable. If False then open the dialog and get the fileName and save it for future reference and set the Boolean to True. Then when they want to save the file again if it's True simply call the SaveFile with the saved FileName variable as the path. Disable any prompts etc...
-
Apr 22nd, 2007, 07:23 PM
#5
Re: save a file (not save as) SAVE
To save the contents of a RTB you call its SaveFile method. If you want to save to the same file you opened then you have to know where that file was. That means assigning the file path to a variable when you open it so that you can retrieve that path later. It would make sense to declare a variable in the form that contains the RTB so when you call the Save method of the form it can simply call the RTB's SaveFile method and pass it that path.
-
Apr 22nd, 2007, 07:26 PM
#6
Re: save a file (not save as) SAVE
Your child form should look something like this:
vb Code:
Private filePath As String
Public Sub Open(ByVal filePath As String)
Me.RichTextBox1.LoadFile(filePath)
Me.filePath = filePath
End Sub
Public Sub Save()
Me.RichTextBox1.SaveFile(Me.filePath)
End Sub
Public Sub SaveAs(ByVal filePath As String)
Me.RichTextBox1.SaveFile(filePath)
Me.filePath = filePath
End Sub
Then your parent form can just get a reference to the active child form, as we discussed in your other thread, and simply call its Open, Save or SaveAs method. The child form itself does the work of opening and saving the files, as is appropriate.
-
Apr 23rd, 2007, 01:42 AM
#7
Thread Starter
Banned
Re: save a file (not save as) SAVE
Theres something wrong with my file save
When it saves the current file the richtextbox shows the path in it and all other contents go away and when i check the contents of the saved file it contains wierd stuff in it like:
Code:
{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\lang1033\f0\fs17 To see The Ultimate Dog Training Guide - A toolkit for training show dogs or any dog. A must for every dog owner,\par
\par
please Copy & Paste this link into the Address Bar of your Browser:\par
http://www.enhancive.net/tr/txt/8/113/\par
provided by James Pinto\par
}
notice the rtf brackets and code at the start and end!?
Code:
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click, SaveToolStripButton.Click
'Dim SaveFileDialog As New SaveFileDialo
If currentFile = "" Then
SaveAsToolStripMenuItem_Click(Me, e)
Exit Sub
End If
Dim child As Form1 = TryCast(Me.ActiveMdiChild, Form1)
Dim strExt As String
strExt = System.IO.Path.GetExtension(currentFile)
strExt = strExt.ToUpper()
Select Case strExt
Case ".RTF"
child.RichTextBox1.SaveFile(currentFile)
Case Else
Dim txtWriter As System.IO.StreamWriter
txtWriter = New System.IO.StreamWriter(currentFile)
txtWriter.Write(child.RichTextBox1.Text)
txtWriter.Close()
txtWriter = Nothing
child.RichTextBox1.SelectionStart = 0
child.RichTextBox1.SelectionLength = 0
child.RichTextBox1.Modified = False
End Select
Dim FileName As String = SaveFileDialog1.FileName
If (child.RichTextBox1.Modified = True And FileName = currentFile) Then
SaveFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
SaveFileDialog1.Filter = "Text Files (*.txt)|*.txt|HTML Files|*.htm|All Files (*.*)|*.*"
If (SaveFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
child.RichTextBox1.SaveFile(SaveFileDialog1.FileName, _
RichTextBoxStreamType.PlainText)
End If
Else
child.RichTextBox1.SaveFile(currentFile)
child.RichTextBox1.Text = FileName & currentFile.ToString()
End If
End Sub
-
Apr 23rd, 2007, 02:29 AM
#8
Re: save a file (not save as) SAVE
 Originally Posted by ethicalhacker
Theres something wrong with my file save
When it saves the current file the richtextbox shows the path in it and all other contents go away and when i check the contents of the saved file it contains wierd stuff in it like:
Code:
{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\lang1033\f0\fs17 To see The Ultimate Dog Training Guide - A toolkit for training show dogs or any dog. A must for every dog owner,\par
\par
please Copy & Paste this link into the Address Bar of your Browser:\par
http://www.enhancive.net/tr/txt/8/113/\par
provided by James Pinto\par
}
notice the rtf brackets and code at the start and end!?
Code:
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click, SaveToolStripButton.Click
'Dim SaveFileDialog As New SaveFileDialo
If currentFile = "" Then
SaveAsToolStripMenuItem_Click(Me, e)
Exit Sub
End If
Dim child As Form1 = TryCast(Me.ActiveMdiChild, Form1)
Dim strExt As String
strExt = System.IO.Path.GetExtension(currentFile)
strExt = strExt.ToUpper()
Select Case strExt
Case ".RTF"
child.RichTextBox1.SaveFile(currentFile)
Case Else
Dim txtWriter As System.IO.StreamWriter
txtWriter = New System.IO.StreamWriter(currentFile)
txtWriter.Write(child.RichTextBox1.Text)
txtWriter.Close()
txtWriter = Nothing
child.RichTextBox1.SelectionStart = 0
child.RichTextBox1.SelectionLength = 0
child.RichTextBox1.Modified = False
End Select
Dim FileName As String = SaveFileDialog1.FileName
If (child.RichTextBox1.Modified = True And FileName = currentFile) Then
SaveFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
SaveFileDialog1.Filter = "Text Files (*.txt)|*.txt|HTML Files|*.htm|All Files (*.*)|*.*"
If (SaveFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
child.RichTextBox1.SaveFile(SaveFileDialog1.FileName, _
RichTextBoxStreamType.PlainText)
End If
Else
child.RichTextBox1.SaveFile(currentFile)
child.RichTextBox1.Text = FileName & currentFile.ToString()
End If
End Sub
Hi,
You can also do it this way;
Code:
Dim bDirty As Boolean = False
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
If strFileName Is Nothing Then
If SaveFileDialog.ShowDialog = DialogResult.OK Then
rtxtEditor.SaveFile(Me.SaveFileDialog.FileName)
strFileName = Me.SaveFileDialog.FileName
End If
Else
rtxtEditor.SaveFile(strFileName)
End If
bDirty = False
End Sub
Wkr,
sparrow1
-
Apr 23rd, 2007, 03:02 AM
#9
Thread Starter
Banned
Re: save a file (not save as) SAVE
 Originally Posted by sparrow1
Hi,
You can also do it this way;
Code:
Dim bDirty As Boolean = False
Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
If strFileName Is Nothing Then
If SaveFileDialog.ShowDialog = DialogResult.OK Then
rtxtEditor.SaveFile(Me.SaveFileDialog.FileName)
strFileName = Me.SaveFileDialog.FileName
End If
Else
rtxtEditor.SaveFile(strFileName)
End If
bDirty = False
End Sub
Wkr,
sparrow1
That did the same thing
If the file contents were
Code:
mp3fusion.net
mp3****s.com
i edited it at runtime adding jam and saved it and now when i open it again it becomes
Code:
{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\lang1033\f0\fs17 mp3fusion.net\par
mp3****s.com\par
jam\par
}
-
Apr 23rd, 2007, 03:21 AM
#10
Re: save a file (not save as) SAVE
 Originally Posted by ethicalhacker
That did the same thing
If the file contents were
Code:
mp3fusion.net
mp3****s.com
i edited it at runtime adding jam and saved it and now when i open it again it becomes
Code:
{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\lang1033\f0\fs17 mp3fusion.net\par
mp3****s.com\par
jam\par
}
Hi,
I've tryed only with the text you gave as example and it works fine for me.
What is 'adding jam' and how do you open the file again.
Wkr,
sparrow1
-
Apr 23rd, 2007, 03:30 AM
#11
Thread Starter
Banned
Re: save a file (not save as) SAVE
 Originally Posted by sparrow1
Hi,
I've tryed only with the text you gave as example and it works fine for me.
What is 'adding jam' and how do you open the file again.
Wkr,
sparrow1
I meant at runtime I type "Jam" to the open file anyway this is my file open procedure Im using the variable currentFile to store the open file name
Code:
Public Sub OpenFile(ByVal sender As Object, ByVal e As EventArgs) Handles OpenToolStripMenuItem.Click, OpenToolStripButton.Click
Dim child As Form1 = TryCast(Me.ActiveMdiChild, Form1)
If child.RichTextBox1.Modified Then
Dim answer As Integer
answer = MessageBox.Show("The current document has not been saved, would you like to continue without saving?", "Unsaved Document(", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If answer = Windows.Forms.DialogResult.No Then
Exit Sub
Else
OpenFile()
End If
Else
OpenFile()
End If
End Sub
Public Sub OpenFile()
Dim child As Form1 = TryCast(Me.ActiveMdiChild, Form1)
OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
OpenFileDialog1.Filter = "Rich Text Files (*.rtf)|*.rtf|Text Files (*.txt)|*.txt|HTML Files|*.htm|All Files (*.*)|*.*"
OpenFileDialog1.FilterIndex = 1
If (OpenFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) And (OpenFileDialog1.FileName.Length > 0) Then
If OpenFileDialog1.FileName = "" Then Exit Sub
Dim strExt As String
strExt = System.IO.Path.GetExtension(OpenFileDialog1.FileName)
strExt = strExt.ToUpper()
Select Case strExt
Case ".RTF"
Dim FileName As String = OpenFileDialog1.FileName
child.RichTextBox1.LoadFile(OpenFileDialog1.FileName, _
RichTextBoxStreamType.PlainText)
Case Else
Dim txtReader As System.IO.StreamReader
txtReader = New System.IO.StreamReader(OpenFileDialog1.FileName)
child.RichTextBox1.Text = txtReader.ReadToEnd
txtReader.Close()
txtReader = Nothing
child.RichTextBox1.SelectionStart = 0
child.RichTextBox1.SelectionLength = 0
End Select
currentFile = OpenFileDialog1.FileName
child.RichTextBox1.Modified = False
End If
End Sub
I open the file again by going to the destination path and double clicking it
the file opens in notepad and that weird stuff gets added to the file along with the modifications
Last edited by ethicalhacker; Apr 23rd, 2007 at 03:34 AM.
-
Apr 23rd, 2007, 03:51 AM
#12
Re: save a file (not save as) SAVE
 Originally Posted by ethicalhacker
I meant at runtime I type "Jam" to the open file anyway this is my file open procedure Im using the variable currentFile to store the open file name
Code:
Public Sub OpenFile(ByVal sender As Object, ByVal e As EventArgs) Handles OpenToolStripMenuItem.Click, OpenToolStripButton.Click
Dim child As Form1 = TryCast(Me.ActiveMdiChild, Form1)
If child.RichTextBox1.Modified Then
Dim answer As Integer
answer = MessageBox.Show("The current document has not been saved, would you like to continue without saving?", "Unsaved Document(", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If answer = Windows.Forms.DialogResult.No Then
Exit Sub
Else
OpenFile()
End If
Else
OpenFile()
End If
End Sub
Public Sub OpenFile()
Dim child As Form1 = TryCast(Me.ActiveMdiChild, Form1)
OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
OpenFileDialog1.Filter = "Rich Text Files (*.rtf)|*.rtf|Text Files (*.txt)|*.txt|HTML Files|*.htm|All Files (*.*)|*.*"
OpenFileDialog1.FilterIndex = 1
If (OpenFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) And (OpenFileDialog1.FileName.Length > 0) Then
If OpenFileDialog1.FileName = "" Then Exit Sub
Dim strExt As String
strExt = System.IO.Path.GetExtension(OpenFileDialog1.FileName)
strExt = strExt.ToUpper()
Select Case strExt
Case ".RTF"
Dim FileName As String = OpenFileDialog1.FileName
child.RichTextBox1.LoadFile(OpenFileDialog1.FileName, _
RichTextBoxStreamType.PlainText)
Case Else
Dim txtReader As System.IO.StreamReader
txtReader = New System.IO.StreamReader(OpenFileDialog1.FileName)
child.RichTextBox1.Text = txtReader.ReadToEnd
txtReader.Close()
txtReader = Nothing
child.RichTextBox1.SelectionStart = 0
child.RichTextBox1.SelectionLength = 0
End Select
currentFile = OpenFileDialog1.FileName
child.RichTextBox1.Modified = False
End If
End Sub
I open the file again by going to the destination path and double clicking it
the file opens in notepad and that weird stuff gets added to the file along with the modifications
Hi,
Here's my way to open a file:
Dim bUpdating As Boolean = False
Code:
Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileOpen.Click
If bDirty = False Then
bUpdating = True
If OpenFileDialog.ShowDialog = DialogResult.OK Then
rtxtEditor.LoadFile(Me.OpenFileDialog.FileName)
strFileName = Me.OpenFileDialog.FileName
bDirty = False
End If
Else
'Prompt to save first
bUpdating = True
Select Case MsgBox("There are unsaved changes. Do you wish to save changes?", _
MsgBoxStyle.YesNo)
Case MsgBoxResult.Yes
mnuFileSave_Click(Me, Nothing)
Case MsgBoxResult.No
'Do nothing, allow open
End Select
If OpenFileDialog.ShowDialog = DialogResult.OK Then
rtxtEditor.LoadFile(Me.OpenFileDialog.FileName)
strFileName = Me.OpenFileDialog.FileName
bDirty = False
End If
bUpdating = False
End If
End Sub
Wkr,
sparrow1
-
Apr 23rd, 2007, 04:25 AM
#13
Thread Starter
Banned
Re: save a file (not save as) SAVE
The code
Code:
child.RichTextBox1.LoadFile(Me.OpenFileDialog1.FileName)
gets highlighted in yellow and I get the error 'File Format is not valid'
-
Apr 23rd, 2007, 04:48 AM
#14
Re: save a file (not save as) SAVE
 Originally Posted by ethicalhacker
The code
Code:
child.RichTextBox1.LoadFile(Me.OpenFileDialog1.FileName)
gets highlighted in yellow and I get the error 'File Format is not valid'
Hi,
I think you know now what the problem is with your text changes. The format isn't valid.
Why are you using a Child.RichTextBox?
Wkr,
sparrow1
-
Apr 23rd, 2007, 04:57 AM
#15
Thread Starter
Banned
Re: save a file (not save as) SAVE
 Originally Posted by sparrow1
Hi,
I think you know now what the problem is with your text changes. The format isn't valid.
Why are you using a Child.RichTextBox?
Wkr,
sparrow1
because im creating an mdi notepad application so to call the current active mdi child and load the file into its richtextbox is use
Code:
Dim child As Form1 = TryCast(Me.ActiveMdiChild, Form1)
Child.RichTextBox1.loadfile()
This was working perfectly before but the save file wasnt so now when i added your code for the saving ang open file Im now getting this error
So now what do I have to do to make it work?
I dint understand the error what file format isnt valid this was working perfectly before
-
Apr 23rd, 2007, 05:16 AM
#16
Re: save a file (not save as) SAVE
Hi,
You can perhaps Save and open the file in you Child form.
Wkr,
sparrow1
-
Apr 23rd, 2007, 05:39 AM
#17
Thread Starter
Banned
Re: save a file (not save as) SAVE
hey there was a silly mistake
there wasnt a
Code:
_
RichTextBoxStreamType.PlainText)
in the code u gave me
Anyway so the file open is working but
ok even the save file is working but its just like before its still saving that crap rtf{} crappy shuff in the file
Last edited by ethicalhacker; Apr 23rd, 2007 at 05:48 AM.
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
|