We have created an Application called MyNotes with a rich text box, a menu with standard items, and a toolstrip. The rich text box fills the form.
Next we will write the code to save the contents of the richtext box when the user selects save from the menu or when they click the save button on the toolbar. We will select one of these events, then write the sub to handle both events.
- Continue with the MyNotes Application.
- Add a SaveFileDialog
control to the form. It will appear under the form and be named SaveFileDialog1.
- Set the Filter property to RichText|*.rtf
- Write the code as shown below:
Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles SaveToolStripButton.Click, SaveToolStripMenuItem.Click
SaveFileDialog1.ShowDialog()
If SaveFileDialog1.FileName <> "" Then 'They did not cancel
RichTextBox1.SaveFile(SaveFileDialog1.FileName)
Me.Text = MyFileName
End If
End Sub
- Click Save All
at this point and run the program.
- Type a few words in the rich text box, then click save.
- Make sure that you remember where you saved it.
- Open the Windows Explorer and find the file. Windows may suggest that you open the file with MSWord. You can also open it with WordPad.
- Next we will write the code to open this same file.