Visual Basic: RichTextBox Control

RichTextBox Control

See Also Example Properties Methods Events

The RichTextBox control allows the user to enter and edit text while also providing more advanced formatting features than the conventional TextBox control.




The RichTextBox control provides a number of properties you can use to apply formatting to any portion of text within the control. To change the formatting of text, it must first be selected. Only selected text can be assigned character and paragraph formatting. Using these properties, you can make text bold or italic, change the color, and create superscripts and subscripts. You can also adjust paragraph formatting by setting both left and right indents, as well as hanging indents.

The RichTextBox control opens and saves files in both the RTF format and regular ASCII text format. You can use methods of the control (LoadFile and SaveFile) to directly read and write files, or use properties of the control such as SelRTF and TextRTF in conjunction with Visual Basic's file input/output statements.

The RichTextBox control supports object embedding by using the OLEObjects collection. Each object inserted into the control is represented by an OLEObject object. This allows you to create documents with the control that contain other documents or objects. For example, you can create a document that has an embedded Microsoft Excel spreadsheet or a Microsoft Word document or any other OLE object registered on your system. To insert objects into the RichTextBox control, you simply drag a file (from the Windows 95 Explorer for example), or a highlighted portion of a file used in another application (such as Microsoft Word), and drop the contents directly onto the control.

The RichTextBox control supports both clipboard and OLE drag/drop of OLE objects. When an object is pasted in from the clipboard, it is inserted at the current insertion point. When an object is dragged and dropped into the control, the insertion point will track the mouse cursor until the mouse button is released, causing the object to be inserted. This behavior is the same as Microsoft Word.

To print all or part of the text in a RichTextBox control use the SelPrint method.

Because the RichTextBox is a data-bound control, you can bind it with a Data control to a Binary or Memo field in a Microsoft Access database or a similar large capacity field in other databases (such as a TEXT data type field in SQL Server).

The RichTextBox control supports almost all of the properties, events and methods used with the standard TextBox control, such as MaxLength, MultiLine, ScrollBars, SelLength, SelStart, and SelText. Applications that already use TextBox controls can easily be adapted to make use of RichTextBox controls. However, the RichTextBox control doesn't have the same 64K character capacity limit of the conventional TextBox control.

*Distribution Note* To use the RichTextBox control in your application, you must add the Richtx32.ocx file to the project. When distributing your application, install the Richtx32.ocx file in the user's Microsoft Windows SYSTEM directory. For more information on how to add a custom control to a project, see the Programmer's Guide.