The complete code is shown below:
Public Class Form1
Dim Changed As Boolean = False
Dim Canceled As Boolean = False
Dim CurrentFile As String = ""
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
If CurrentFile = "" Then
SaveFileDialog1.ShowDialog()
CurrentFile = SaveFileDialog1.FileName
End If
If CurrentFile <> "" Then 'They did not cancel
RichTextBox1.SaveFile(CurrentFile)
Me.Text = CurrentFile
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
CurrentFile = OpenFileDialog1.FileName
RichTextBox1.LoadFile(CurrentFile)
Me.Text = CurrentFile
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 = CurrentFile 'Set opened file to default
SaveFileDialog1.ShowDialog()
If SaveFileDialog1.FileName <> "" Then 'They did not cancel
CurrentFile = SaveFileDialog1.FileName
RichTextBox1.SaveFile(CurrentFile)
Me.Text = CurrentFile
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"
CurrentFile = "" '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
Private Sub ToolStripBold_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripBold.Click, ToolStripItalics.Click
If Not RichTextBox1.SelectionFont Is Nothing Then
Dim currentFont As System.Drawing.Font = RichTextBox1.SelectionFont
Dim newFontStyle As System.Drawing.FontStyle
Dim italic As FontStyle
Dim bold As FontStyle
If RichTextBox1.SelectionFont.Bold = True Then
bold = FontStyle.Bold
End If
If RichTextBox1.SelectionFont.Italic = True Then
italic = FontStyle.Italic
End If
If sender Is ToolStripBold Then
If RichTextBox1.SelectionFont.Bold = True Then
bold = FontStyle.Regular
Else
bold = FontStyle.Bold
End If
End If
If sender Is ToolStripItalics Then
If RichTextBox1.SelectionFont.Italic = True Then
italic = FontStyle.Regular 'turn it off
Else
italic = FontStyle.Italic 'turn it on
End If
End If
newFontStyle = bold + italic
RichTextBox1.SelectionFont = New Font(currentFont.FontFamily, currentFont.Size, newFontStyle)
End If
End Sub
End Class
Experiment: This is a fairly complete application. You can add additional touches such as changing the font and size, add an about box or a splash screen.