Zebra0.com

visual-basic richtext

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.

Following the example from the ToDo list project, we will modify the code to ask the user if they want to save if there is a change to the rich text box and we do any of the following:

  1. Continue with the MyNotes Application.
  2. Modify the code as shown below:
  3. Public Class Form1
        Dim Changed As Boolean = False
        Dim Canceled As Boolean = False
        Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
            ResizeRichText()
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Text = "untitled.rtf"
            ResizeRichText()
        End Sub
        Private Sub ResizeRichText()
            RichTextBox1.Width = Me.Width
            RichTextBox1.Height = Me.Height - RichTextBox1.Location.Y
        End Sub
    
        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
                Changed = False
            End If
        End Sub
    
        Private Sub OpenToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles OpenToolStripButton.Click, OpenToolStripMenuItem.Click
            If Changed Then
                AskToSave()
            End If
            If Not Canceled Then
                OpenFileDialog1.ShowDialog()
                If OpenFileDialog1.FileName <> "" Then 'They did not cancel
                    RichTextBox1.LoadFile(OpenFileDialog1.FileName)
                    Me.Text = OpenFileDialog1.FileName
                    Changed = False
                End If
            End If
        End Sub
    
        Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
            saveas()
        End Sub
        Private Sub saveas()
            SaveFileDialog1.FileName = OpenFileDialog1.FileName 'Set opened file to default
            SaveFileDialog1.ShowDialog()
            If SaveFileDialog1.FileName <> "" Then 'They did not cancel
                RichTextBox1.SaveFile(SaveFileDialog1.FileName)
                Me.Text = SaveFileDialog1.FileName
                Changed = False
            End If
        End Sub
    
        Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
          Handles NewToolStripMenuItem.Click, NewToolStripButton.Click
            If Changed Then
                AskToSave()
            End If
            If Not Canceled Then
                RichTextBox1.Text = ""
                Me.Text = "Untitled"
                OpenFileDialog1.FileName = "" 'If they click save we want to show the SaveFileDialog
                Changed = False
            End If
        End Sub
        Private Sub AskToSave()
            Dim Answer As MsgBoxResult
            If Changed Then
                Answer = MsgBox("Do you want to save?", _
                  MsgBoxStyle.YesNoCancel + MsgBoxStyle.Question, "Save")
                If Answer = MsgBoxResult.Yes Then
                    saveas()
                End If
                If Answer = MsgBoxResult.Cancel Then
                    Canceled = True
                End If 'Do NOT proceed if they cancel!
            End If
        End Sub 'AskToSave 
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) _
      Handles Me.FormClosing
            Canceled = False
            If Changed Then
                AskToSave()
                If Canceled Then
                    e.Cancel = True 'Program does NOT end!
                End If
            End If
        End Sub 'Form1_FormClosing
    
        Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
            Changed = True
        End Sub
    End Class
    
  4. Click Save All SaveAllat this point and run the program.
  5. Try all of the events shown above and make sure that the program behaves the way that you expect.

What is missing at this point is the implementation to add formating to the text.

NEXT: Add a ToolBar