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!).
- Continue with the MyNotes Application.
- Add another ToolStrip
It will be right below the first tool strip.
- Add a button to the tool strip called ToolStripBold.
- Download this picture for bold
and add it to the button.
- Add another button called ToolStripItalic and use this picture:
.
- Write the code as shown below:
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
- Click Save All
at this point and run the program.
- Open one of the rtf files you created, select some text and make it bold, italic or both. Experiment!
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.