Join 137,234 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,640 people online right now. Registration is fast and FREE... Join Now!
I am trying to save a file that I create with the RichTextBox and when i go to save the file (and the file already exists) is doesn't save as a new file but instead adds to the existing file. So basically I end up with double the information, one with the original file (that's supposedly was suppose to be saved over) and then the original file with the added changes!
Here's the code:
CODE
Dim Save As New SaveFileDialog() Dim myStreamWriter As System.IO.StreamWriter
Try myStreamWriter = System.IO.File.AppendText(Save.FileName) myStreamWriter.Write(RichTextBox.Text) myStreamWriter.Close() Catch ex As Exception 'Do Nothing End Try End Sub
Basically, I want to be able to save over a file and not have it double the information. Any help would be great. Thanks.
How do I go about setting up the program to automatically save to that saved file? Forexample say I have File.rtf, how do I, when I hit the Ctrl+S, automatically save to that particular file, File.rtf? And also, how would I go about setting up a Save As... command? These two things are bugging the crap out of me, any help would be greatly appreciated. Thanks!!
How do I go about setting up the program to automatically save to that saved file? Forexample say I have File.rtf, how do I, when I hit the Ctrl+S, automatically save to that particular file, File.rtf? And also, how would I go about setting up a Save As... command? These two things are bugging the crap out of me, any help would be greatly appreciated. Thanks!!
If anybody can help me with this, I would greatly appreciate it. I've been tying for at least a week and a half and I am coming up blank.
Are you using a menustrip on the top of your form?
If so, then set the shortcut for the Save menu item to Ctrl-S. You might want to create two seperate procedures one for the Save and a seperate on for the Save As.
To create a SaveAs dialog it is the same as the code you have for the Save dialog in your first post. Just change the title to say "Save As".
Are you using a menustrip on the top of your form?
If so, then set the shortcut for the Save menu item to Ctrl-S. You might want to create two seperate procedures one for the Save and a seperate on for the Save As.
To create a SaveAs dialog it is the same as the code you have for the Save dialog in your first post. Just change the title to say "Save As".
Thanks for responding. I have set the shortcut for the two command, Save and Save As. I have set the same coding for the Save As as I did with the Save. But when I have saved a file that I am currently working on, it opens up the SaveDialog and asks to save it over again. Whereas I want to accomplish the task of it automatically saving it to the currently opened file. How would I go about doing that? Thanks.
Don't use the SaveFileDialog when you click Save, you only need it in the Save As event.
All you need to do is just save the file.
Code inside the Save click event:
CODE
Dim myStreamWriter As System.IO.StreamWriter
Try myStreamWriter = System.IO.File.CreateText(Save.FileName) myStreamWriter.Write(RichTextBox.Text) myStreamWriter.Close() Catch ex As Exception 'Do Nothing End Try
Don't use the SaveFileDialog when you click Save, you only need it in the Save As event.
All you need to do is just save the file.
Code inside the Save click event:
CODE
Dim myStreamWriter As System.IO.StreamWriter
Try myStreamWriter = System.IO.File.CreateText(Save.FileName) myStreamWriter.Write(RichTextBox.Text) myStreamWriter.Close() Catch ex As Exception 'Do Nothing End Try
When I do that, it is not recognizing the save in "myStreamWriter = System.IO.File.CreateText(Save.FileName)", what should i do? Thanks.