The RichTextBox control provides properties for manipulating the selected text on the control. The names of these properties start with the Selection or Selected prefix, and the most commonly used ones are shown in Table 4.5. Some of these properties are discussed in further detail in following sections.
Table 4.5 – RichTextBox Properties for Manipulating Selected Text
Property | What It Manipulates |
---|---|
SelectedText | The selected text |
SelectedRtf | The RTF code of the selected text |
SelectionStart | The position of the selected text’s first character |
SelectionLength | The length of the selected text |
SelectionFont | The font of the selected text |
SelectionColor | The color of the selected text |
SelectionBackColor | The background color of the selected text |
SelectionAlignment | The alignment of the selected text |
SelectionIndent, SelectionRightIndent, SelectionHangingIndent | The indentation of the selected text |
RightMargin | The distance of the text’s right margin from the left edge of the control |
SelectionTabs | An array of integers that sets the tab stop positions in the control |
SelectionBullet | Whether the selected text is bulleted |
BulletIndent | The amount of bullet indent for the selected text |
SelectedText
The SelectedText property represents the selected text, whether it was selected by the user via the mouse or from within your code. To assign the selected text to a variable, use the following statement:
selText = RichTextbox1.SelectedText
Code language: VB.NET (vbnet)
You can also modify the selected text by assigning a new value to the SelectedText property. The following statement converts the selected text to uppercase:
RichTextbox1.SelectedText = RichTextbox1.SelectedText.ToUpper
Code language: VB.NET (vbnet)
You can assign any string to the SelectedText property. If no text is selected at the time, the statement will insert the string at the location of the pointer.
SelectionStart, SelectionLength
To simplify the manipulation and formatting of the text on the control, two additional properties, SelectionStart and SelectionLength, report (or set) the position of the first selected character in the text and the length of the selection, respectively, regardless of the formatting of the selected text. One obvious use of these properties is to select (and highlight) some text on the control:
RichTextBox1.SelectionStart = 0
RichTextBox1.SelectionLength = 100
Code language: VB.NET (vbnet)
You can also use the Select method, which accepts as arguments the starting location and the length of the text to be selected.
SelectionAlignment
Use this property to read or change the alignment of one or more paragraphs. This property’s value is one of the members of the HorizontalAlignment enumeration: Left, Right, and Center. Users don’t have to select an entire paragraph to align it; just placing the pointer anywhere in the paragraph will do the trick, because you can’t align part of the paragraph.
SelectionIndent, SelectionRightIndent, SelectionHangingIndent
These properties allow you to change the margins of individual paragraphs. The SelectionIndent property sets (or returns) the amount of the text’s indentation from the left edge of the control. The SelectionRightIndent property sets (or returns) the amount of the text’s indentation from the right edge of the control. The SelectionHangingIndent property indicates the indentation of each paragraph’s first line with respect to the following lines of the same paragraph. All three properties are expressed in pixels.
The SelectionHangingIndent property includes the current setting of the SelectionIndent property. If all the lines of a paragraph are aligned to the left, the SelectionIndent property can have any value (this is the distance of all lines from the left edge of the control), but the SelectionHangingIndent property must be zero. If the first line of the paragraph is shorter than the following lines, the SelectionHangingIndent has a negative value. Figure 4.18 shows several differently formatted paragraphs. The settings of the SelectionIndent and SelectionHangingIndent properties are determined by the two sliders at the top of the form.

Figure 4.18 – Various combinations of the SelectionIndent and SelectionHangingIndent properties produce all possible paragraph styles.
SelectionBullet, BulletIndent
You use these properties to create a list of bulleted items. If you set the SelectionBullet property to True, the selected paragraphs are formatted with a bullet style, similar to the <ul> tag in HTML. To create a list of bulleted items, select them from within your code and assign the value True to the SelectionBullet property. To change a list of bulleted items back to normal text, make the same property False.
The paragraphs formatted as bullets are also indented from the left by a small amount. To set the amount of the indentation, use the BulletIndent property, which is also expressed in pixels.
SelectionTabs
Use this property to set the tab stops in the RichTextBox control. The Selection tab should be set to an array of integer values, which are the absolute tab positions in pixels. Use this property to set up a RichTextBox control for displaying tab-delimited data.
Methods Of the RichTextBox control
The first two methods of the RichTextBox control you need to know are SaveFile and LoadFile. The SaveFile method saves the contents of the control to a disk file, and the LoadFile method loads the control from a disk file.
SaveFile
The syntax of the SaveFile method is as follows:
RichTextBox1.SaveFile(path, filetype)
Code language: VB.NET (vbnet)
where path is the path of the file in which the current document will be saved. By default, the SaveFile method saves the document in RTF format and uses the .RTF extension. You can specify a different format by using the second optional argument, which can take on the value of one of the members of the RichTextBoxStreamType enumeration, described in Table 4.6.
Table 4.6 – The RichTextBoxStreamType Enumeration
Format | Effect |
---|---|
PlainText | Stores the text on the control without any formatting |
RichNoOLEObjs | Stores the text without any formatting and ignores any embedded OLE objects |
RichText | Stores the text in RTF format (text with embedded RTF commands) |
TextTextOLEObjs | Stores the text along with the embedded OLE objects |
UnicodePlainText | Stores the text in Unicode format |
LoadFile
Similarly, the LoadFile method loads a text or RTF file to the control. Its syntax is identical to the syntax of the SaveFile method:
RichTextBox1.LoadFile(path, filetype)
Code language: VB.NET (vbnet)
The filetype argument is optional and can have one of the values of the RichTextBoxStreamType enumeration. Saving and loading files to and from disk files is as simple as presenting a Save or Open common dialog to the user and then calling one of the SaveFile or LoadFile methods with the filename returned by the common dialog box.
Select, SelectAll
The Select method selects a section of the text on the control, similar to setting the SelectionStart and SelectionLength properties. The Select method accepts two arguments: the location of the first character to be selected and the length of the selection:
RichTextBox1.Select(start, length)
Code language: VB.NET (vbnet)
The SelectAll method accepts no arguments and it selects all the text on the control.