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 name the selected text bold or italics (or both!).
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
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.
INDEX, The Rich Text Box and a Menu, Add a ToolBar, Resize the RichTextBox to fill the Form, Save the contents of the RichText Box, Open a File, Save As, New, Ask to Save, Formatting the Rich Text Box, Code for the complete Rich Text Box Application