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?
Printable View
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?
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()Quote:
Originally Posted by stimbo
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...
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.
Your child form should look something like this: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.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
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:
notice the rtf brackets and code at the start and end!?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
}
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,Quote:
Originally Posted by ethicalhacker
You can also do it this way;
Wkr,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
sparrow1
That did the same thingQuote:
Originally Posted by sparrow1
If the file contents were
i edited it at runtime adding jam and saved it and now when i open it again it becomesCode:mp3fusion.net
mp3****s.com
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,Quote:
Originally Posted by ethicalhacker
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 nameQuote:
Originally Posted by sparrow1
I open the file again by going to the destination path and double clicking itCode: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
the file opens in notepad and that weird stuff gets added to the file along with the modifications
Hi,Quote:
Originally Posted by ethicalhacker
Here's my way to open a file:
Dim bUpdating As Boolean = False
Wkr,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
sparrow1
The codegets highlighted in yellow and I get the error 'File Format is not valid'Code:child.RichTextBox1.LoadFile(Me.OpenFileDialog1.FileName)
Hi,Quote:
Originally Posted by ethicalhacker
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 useQuote:
Originally Posted by sparrow1
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 errorCode:Dim child As Form1 = TryCast(Me.ActiveMdiChild, Form1)
Child.RichTextBox1.loadfile()
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
Hi,
You can perhaps Save and open the file in you Child form.
Wkr,
sparrow1
hey there was a silly mistake
there wasnt ain the code u gave meCode:_
RichTextBoxStreamType.PlainText)
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