File Handling: Adding a Save As Command

In this tutorial, we’re going to focus on how to add a “Save As” command. Since we have already a save command, we still need to add a “Save As” command because the Save command automatically saves the file, while Save As opens a dialog box which can be used to change the name, location of a file as well as the format of a file. The save and Save As command is essentially the same, that is to save the document. But, Save and Save As has a little difference. The Save Command automatically saves the file using the same name, Location or format of a file as when it was opened or last saved. However, the save as command opens a dialog box in which user can change the file, format as well as the location of where the file is saved. To start with this application, open our previous project called “File Handling”. Then under the sub menu, add another menu called “Save As”. Then double click this menu and add the following code:
  1. 'show the savefiledialog
  2. SaveFileDialog1.ShowDialog()
  3. 'if the user encode the file and click save
  4. If SaveFileDialog1.ShowDialog = DialogResult.OK Then
  5. 'it create a streamwriter to write in to the file
  6. Dim writer As New System.IO.StreamWriter(SaveFileDialog1.FileName, False)
  7. 'get the content of Richtextbox and write to a stream
  8. writer.Write(RichTextBox1.Text)
  9. 'close the writer
  10. writer.Close()
  11. 'it gets the data contains by the control and save the filename for later use.
  12. RichTextBox1.Tag = SaveFileDialog1.FileName
  14. End If
Then press “F5” to run this application. After this open the last example called “Brownfox”. And it will now look like as shown below: Then under file menu, select “Save As”. And it will look like as shown below: After clicking this command, the dialog box will open and on the file name write “newBrownFox.doc”, because we’re going to save it as a word document. And finally click save. Then, if we will look at the my Documents, you can see there the new file called “newBrownFox.doc” in a Microsoft Word Document. And it looks like as shown below: Finally we’re done for this tutorial, and if want to review about file handling, you can follow this our previous tutorial called “File handling: Writing and Saving a Text File”.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
3 + 10 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.