Character Formatting

Gives the selected characters of a RichTextBox a font, a size, a color and the effects bold, italic, underline and strikethrough, raises them above the line or lowers them below it, and hides them.

RichTextBox1.Text = "Title and text"
RichTextBox1.SelStart = 0
RichTextBox1.SelLength = 5
RichTextBox1.SelFontSize = 14
RichTextBox1.SelUnderline = True
RichTextBox1.SelColor = vbBlue
Debug.Print RichTextBox1.SelFontSize; RichTextBox1.SelUnderline; RichTextBox1.SelColor = vbBlue
RichTextBox1.SelStart = 6
RichTextBox1.SelLength = 3
Debug.Print RichTextBox1.SelUnderline     ' "and" is not underlined
' Output:
'  14 TrueTrue
' False

The format of the selection

Each property on this page reads and sets one part of the format of the selected characters. With nothing selected, it reads the format at the insertion point and sets the format of the text typed there next.

Each is a Variant. While the selection holds characters that differ in that part of the format, such as bold and not bold, it returns Null. A value of a type that the property does not take, such as a string for SelBold, raises run-time error 13, Type mismatch.

The effects SelBold, SelItalic, SelStrikethru and SelUnderline take True or False, or a number, of which any but 0 is True. An assignment of Null to one of them gives the selection that effect, as True does.

RichTextBox1.Text = "bold and plain"
RichTextBox1.SelStart = 0
RichTextBox1.SelLength = 4
RichTextBox1.SelBold = True
RichTextBox1.SelLength = 14
Debug.Print IsNull(RichTextBox1.SelBold)     ' some characters are bold, some are not
RichTextBox1.SelBold = Null                 ' all of them bold
Debug.Print RichTextBox1.SelBold
' Output:
' True
' True

Hidden text

With SelVisible set to False the selected characters are not shown, and take no room. They stay part of the text: Text and TextLength count them.

Properties

SelBkColor

The color behind the selected characters. A Variant that holds a color, which the property returns as an RGB value, or Null. Text with no color of its own behind it shows the control’s BackColor.

SelBold

Whether the selected characters are bold. A Variant: True, False or Null. See The format of the selection.

SelCharOffset

How far the selected characters are raised above the line of the text, or lowered below it with a negative value, in the scale units of the control’s container. A Variant that holds a number, or Null.

SelColor

The color of the selected characters. A Variant that holds a color, such as vbRed or the value of RGB, or Null. The property returns the color as an RGB value, also when a color of the system, such as vbWindowText, was assigned.

SelFontCharset

The character set of the font of the selected characters, such as 0 for the ANSI character set. A Variant that holds a number from 0 to 255, or Null. Of a greater number the control keeps the lowest 8 bits.

SelFontName

The name of the font of the selected characters. A Variant that holds a String, or Null. A name of more than 31 characters is cut to 31.

SelFontSize

The size of the font of the selected characters, in points. A Variant that holds a number, or Null. The property returns a Single.

SelItalic

Whether the selected characters are in italics. A Variant: True, False or Null.

SelStrikethru

Whether the selected characters are struck through with a line. A Variant: True, False or Null.

SelUnderline

Whether the selected characters are underlined. A Variant: True, False or Null.

SelVisible

Whether the selected characters are shown. A Variant: True, False or Null. See Hidden text. An assignment of Null shows them, as True does.

See Also