The RichTextBox Control
The RichTextBox control allows the user to display, enter, and edit text while also providing more advanced formatting features than the conventional TextBox control.
The RichTextBox (RTB) 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 RTB also supports the embedding of images.
The RTB 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 in conjunction with Visual Basic's file input/output statements.
The sample application presented here is a “WordPad”-like text editor (see the screen shot below). This application shows you how to use many of the RichTextBox's features and also incorporates the use of menus, toolbars, dialogs, and the status bar presented in previous tutorials.
There are two downloads for this sample application. The first one does not support printing; the second one does. The reason for this is explained below:
In pre-.NET versions of Visual Basic (VB6 and earlier), the RTB had a method called SelPrint which enabled you to print all or part of the RTB content. This method is not supported in VB.NET / VB 20XX. Thus, the RTB does not have a built-in way to print itself. To accomplish the task of printing the RTB contents, you must create a custom control that inherits the RTB and extends the RichTextBox class to use the EM_FORMATRANGE message. You can then send the content of a RichTextBox to an output device, such as a printer. This is described in the Microsoft article: http://support.microsoft.com/kb/811401. The methodology described in this article has been implemented in the second download for this application.
Download the project files for the first version of the sample application (uses “regular” RTB, does not support printing) here.
Download the project files for the second version of the sample application (uses custom / extended RTB, supports printing) here.