Zebra Notes: Open, Save, Save As, and Format with a Rich Text Box

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.

  1. Continue with the MyNotes Application.
  2. Add a SaveFileDialog SaveFileDialog control to the form. It will appear under the form and be named SaveFileDialog1.
  3. Set the Filter property to RichText|*.rtf
  4. Write the code as shown below:
  5. 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 
  6. Click Save All SaveAllat this point and run the program.
  7. Type a few words in the rich text box, then click save.
  8. Make sure that you remember where you saved it.
  9. 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.
  10. Next we will write the code to open this same file.

NEXT: Open a File