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.

Next we will write the code to name the selected text bold or italics (or both!).

  1. Continue with the MyNotes Application.
  2. Add another ToolStrip toolstripIt will be right below the first tool strip.
  3. Add a button to the tool strip called ToolStripBold.
  4. Download this picture for bold bold and add it to the button.
  5. Add another button called ToolStripItalic and use this picture: italic.
  6. Write the code as shown below:
  7.   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
  8. Click Save All SaveAllat this point and run the program.
  9. 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.

NEXT: Add a ToolBar