RichTextBox Scenario : Change Fonts, FontSize, and FontColor
Collapse the table of content
Expand the table of content

Visual Basic Concepts

Visual Studio 6.0

RichTextBox Scenario: Change Fonts, FontSize, and FontColor

With the RichTextBox control the end user can change various attributes of text such as font, font size, and font color. In this scenario, three ComboBox controls appear on an application's toolbar to show attributes of text in the RichTextBox control:

Using these ComboBox controls, the user can select a font, font color, and font size to apply to selected text.

The following objects are used in this scenario:

  • Form object named "frmRTF"

  • Toolbar control named "tlbRTF"

  • ComboBox control named "cmbFontColor"

  • ComboBox control named "cmbFontName"

  • ComboBox control named "cmbFontSize"

  • RichTextBox control named "rtfData"

Note   The following steps are a general outline of the creation process. Each step is explained in greater detail later in this topic.

To create three combo boxes that display attributes

  1. Set the Form object's ScaleMode to centimeters.

  2. Create a Toolbar control.

  3. Create three ComboBox controls on the Toolbar control.

  4. In the Form object's Load event, populate the ComboBox controls.

  5. In the ComboBox controls' Click event, set the SelFontName, SelFontColor, or SelFontSize property.

  6. In the RichTextBox control's SelChange event, use the SelFontName, SelFontColor, or SelFontSize property to change the appropriate ComboBox control's List property.

Set the Form Object's ScaleMode to Centimeters

The Form object's ScaleMode property determines which measurement unit is used for certain properties. On the RichTextBox control, the SelIndent. SelHangingIndent, and other properties, use the ScaleMode unit. For these properties, it's more convenient to use centimeters rather than twips. In addition, when creating Button objects with the PlaceHolder style, the ScaleMode property determines how the Button object's width is measured.

To set the form object's ScaleMode property to centimeters

  1. Click the Form object.

  2. Press F4 to display the Properties window.

  3. Click the ScaleMode property and select 7 (centimeters).

Create a Toolbar Control

On the Toolbox, double-click the Toolbar control icon. In the Properties window, double-click Name and change the name of the control to tlbRTF.

Create Three ComboBox Controls

Three ComboBox controls will be placed on the Toolbar. Each ComboBox control will be populated with font attributes.

To create three ComboBox controls

  1. On the Toolbox, click the ComboBox control icon.

  2. Draw a ComboBox control on the Toolbar control.

  3. Set the Name property for each of the three ComboBox controls according to the following table:
    ComboBox Property Value
    ComboBox1 Name cmbFontName
    ComboBox2 Name cmbFontSize
    ComboBox3 Name cmbFontColor

Populate the ComboBox Controls

The ComboBox controls must also be populated with appropriate values. The following code populates each ComboBox control in the Form_Load event.

Private Sub Form_Load()
   ' Add colors to cmbFontColor.
   With cmbFontColor
   .AddItem "Black"
   .AddItem "Blue"
   .AddItem "Red"
   .AddItem "Green"
   .ListIndex = 0
   End With

   Dim i As Integer
   With cmbFontName
      For i = 0 to Screen.FontCount - 1
         .AddItem Screen.Fonts(i)
      Next i
      ' Set ListIndex to 0.
      .ListIndex = 0
   End With

   With cmbFontSize
      ' Populate the combo with sizes in
      ' increments of 2.
      For i = 8 To 72 Step 2
         .AddItem i
      Next i
      ' Set ListIndex to 0
      .ListIndex = 1 ' size 10.
   End With
End Sub

In the ComboBox Controls' Click event: Set the SelFontName, SelColor, or SelFontSize Property

To set the font, color, and font size for a RichTextBox control, use the SelFontName, SelColor, and SelFontSize properties. For each of the ComboBox controls, set the appropriate property in the Click event. After setting the property, you may want to set the focus back to the RichTextBox control:

Private Sub cmbFontName_Click()
   rtfData.SelFontName = cmbFont
   rtfData.SetFocus
End Sub

Private Sub cmbFontSize_Click()
   rtfData.SelFontSize = cmbFontSize.Text
   rtfData.SetFocus
End Sub

Private Sub cmbFontColor_Click()
   ' Change font colors of text using the
   ' Select Case statement with the ListIndex of the
   ' ComboBox control. Set the colors with
   ' the intrinsic constants for color.
   Me.Show
      With rtfData
   Select Case cmbFontColor.ListIndex
   Case 0
      .SelColor = vbBlack
   Case 1
      .SelColor = vbBlue
   Case 2
      .SelColor = vbRed
   Case 3
      .SelColor = vbGreen
   End Select
   End With
   ' Return focus to the RichTextbox control.
   rtfData.SetFocus
End Sub

In the SelChange Event: Use the SelFontName, SelColor, or SelFontSize property to change the appropriate ComboBox control's List property

The ComboBox control can also be used to notify the user of text attributes as the insertion point is moved in the RichTextBox control. The SelChange event occurs either when the insertion point is moved, or when selected text is changed. Thus, in the SelChange event, check the SelFontName, SelFontColor, and SelFontSize properties and reset the appropriate ComboBox control.

Private Sub rtfData_SelChange()
   ' SelFontSize returns the font size, or Null if
   ' it's mixed.
   If Not IsNull(rtfData.SelFontSize) Then
      cmbFontSize.Text = rtfData.SelFontSize
   End If

   ' Show Font name in the ComboBox.
   cmbFont.Text = rtfData.SelFontName

' Show color of text in the ComboBox. Use the
' intrinsic constants for color to determine
' the color of the text.
   Select Case rtfData.SelColor
      Case vbBlack
         cmbFontColor.ListIndex = 0
      Case vbBlue
         cmbFontColor.ListIndex = 1
      Case vbRed
         cmbFontColor.ListIndex = 2
      Case vbGreen
         cmbFontColor.ListIndex = 3
   End Select
End Sub

Show:
© 2016 Microsoft