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 load a file into the richtext box when the user selects open from the menu or when they click the open 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 OpenFileDialog open file dialog control to the form. It will appear under the form and be named OpenFileDialog1.
  3. Set the Filter property to RTF|*rtf|
  4. Write the code as shown below:
  5. Private Sub OpenToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles OpenToolStripButton.Click, OpenToolStripMenuItem.Click
        OpenFileDialog1.ShowDialog()
        If OpenFileDialog1.FileName <> "" Then 'They did not cancel
           RichTextBox1.LoadFile(OpenFileDialog1.FileName)
           Me.Text = OpenFileDialog1.FileName
        End If
    End Sub 
  6. Click Save All SaveAllat this point and run the program.
  7. Open the file that you created earleir, then click save. Although we just opened the file, it prompts us for the location to save the file.
  8. We will modify Save as shown below:
  9. Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
      Handles SaveToolStripButton.Click, SaveToolStripMenuItem.Click
    Dim MyFileName As String = OpenFileDialog1.FileName
    If MyFileName = "" Then
    SaveFileDialog1.ShowDialog()
    MyFileName = SaveFileDialog1.FileName
    End If
    If MyFileName <> "" Then 'They did not cancel
    RichTextBox1.SaveFile(MyFileName)
    Me.Text = MyFileName
    End If
    End Sub
  10. Click Save All SaveAllat this point.

NEXT: Save As