More Advanced ActiveX Controls VB Help

This chapter explores a few of the more advanced ActiveX controls that you’ll find useful in building elaborate applications such as word processors and spreadsheets, and they are:

  1. The RichTextBox control, the core of a word processor, wrapped as an ActiveX control
  2. The MSFlexGrid control, the core of a spreadsheet application, also wrapped as an ActiveX control

The RichTextBoxcontrol provides all the functionality you’ll need to build a word processor, and the MSFlexGrid control provides the necessary functionality for a spreadsheet application. To add data plotting features to your applications, especially applications based on the MSFlexGrid control, you can use the MSChart control, which is presented in Appendix Be on the CD.

The RichTextBox Control

The RichTextBox control is the core of a full-blown word processor. It provides all the functionality of a TextBox control; it gives you the capability to mix different fonts, sizes, and attributes; and it gives you precise control over the margins of the text (see Figure 9.1). You can even place images in your text on a RichTextBox control (although you won’t have the kind of control over the embedded images that you have with Word 97).

The fundamental property of the Rich TextBox control is its TextRTF property. Similar to the Text property of the TextBox control, this property is the text currently displayed by the control. Unlike the Text property, which returns (or sets) the text of the control but doesn’t contain formatting information, the TextRTF property returns the text along with any formatting information, Therefore, you can use the RichTextBox control to specify the text’s formatting including paragraph indentation, font, and font size or style, among other things .



RTF stands for Rich Text Format, which is a standard for storing formatting information along with the text. The beauty of the RichTextBox control for programmers is that they don’t need to supply the formatting codes. The control provides simple properties that turn the selected text into bold, change the alignment of the current paragraph, and so on. The RTF code is generated internally by the control and used to save and load formatted files. It’s possible to create elaborately formatted documents without knowing the RTF language.

The RTFLanguage

A basic knowledge of the RTF format, its commands, and how it works, will certainly help you understand how the RichTextBox control works. RTF is a document formatting language that uses simple commands to specify the formatting of a document. These commands, or tags, are Ascii strings, such as \ par (the tag that marks the beginning of a new paragraph) and \b (the tag that turns-on the bold style). And this is where the value of the RTF format lies. RTF documents don’t .

contain special characters and can be easily exchanged among different operating systems and computers, as long as there is an RTF- capable application to read the document. Let’s look at the RTF document in action.

Open the WordPad application (choose Start > Programs > Accessories > WordPad) and enter a few lines of text (see Figure 9.2). Select a few words or sentences and format them-in different ways with any of WordPad’s formatting commands. Then save the document in RTF format. Choose File >Save As, select Rich TextFormat, and then save the file as Document.rtf. If you open this file with a text editor such as NotePad, you’ll see the actual RTFcode that produced the document. You can find the RTF file for the document shown in Figure 9.2 in this chapter’s folder on the CD.



As you can see, all formatting tags are prefixed with the backslash (~) symbol. To display the \ symbol itself, insert an additional slash. Paragraphs are marked with the \par tag, and the entire document is enclosed in a pair of curly brackets. The \li and \ri tags followed by a numeric value specify the amount of the left and right indentation. Hyou assign this string to the TextRTF property of a Rich- TextBox control, the result will be the document shown in Figure 9.2,  formatted Exactly it appears in WordPad. .

RTF is similar to HTML (Hypertext Markup Language), and if you’re familiar with HTML, a few comparisons between the two standards will provide helpful hints and insight into the RTF language. Like HTML, RTF was designed to create formatted documents that could be displayed on different systems. The RTF language uses tags to describe the document’s format. For example, the tag for italics is \i, and its scope is delimited with a pair of curly brackets. The following RTF segment displays a sentence with a few words in italics:

{{\b RTF} (which stands for Rich Text Format) is a {\i document
formatting language} that uses simple commands to specify the
formatting of the document.}

The following is the equivalent HTML code:

<B>RTF</B> (which stands for Rich Text Format) is a <I>document formatting language</I> that uses simple commands to specify the formatting of the document.

The <B> and <I> tags of HTML are equivalent to the band \i tags of RTF. RTF, however, is much more complicated. than HTML. It’s not nearly as easy to understand an RTF document as it is to understand an Hl’ML document because RTF was meant to be used internally by applications. As you can see in tli.eRTF segment presented earlier, RTF contains information about the font being used, its size, and so on. Just as you need a browser to view HTML documents, you need an RTF-capable application to view RTF documents. WordPad, for instance, supports RTF and can both save a document in RTF format and read RTF files.

You’re not expected to supply your own RTF code to produce a document. You simply select the segment of the document you want to format and apply the corresponding formatting command from within your word processor. Fortunately, the RichTextBox control isn’t any different. It doesn’t require you or the users of your application to understand RTF code. The RichTextBox control does all the work for you while hiding the low-level details.

VB6 at Work: The RTFDemo Application

The RTFDemo application, shown in Figure 9.3, demonstrates the principles of programming the RichTextBox control. The RichTextBox control is the large box in the upper section of the Form where you can type text as you would with a regular TextBox control.



Use the buttons on the top row to set styles for the selected text. The Bold and Italic buttons are self-explanatory; the Regular button restores the regular style of the text. All three buttons set (or reset) the value of the properties Sel Bold (for the bold attribute) and Sel Italic (for the italic attribute). By setting one of these properties to True, the corresponding attribute is turned on for the selected text; if set to False, it will be turned off. Here’s the code behind the Bold button:

RichTextBox1.SelBold = True
The code for the Italic button is:
RichTextBoxl.Selltalic = True

The Regular button’s code contains the following two lines:

RichTextBox1.SelBold = False
RichTextBox1.Selltalic = False

The Clear button clears the contents of the control by setting its Text property to an empty string:

RichTextBox1.Text = ‘ ‘

The two buttons on the second row demonstrate the nature of the RichTextBox control. Select a few words on the control, turn on their bold and/or italic attribute, and then click the Show Text button. You’ll see a message box that contains the control’s text. No matter how the text is formatted the control’s Text property will be the same. This is the text you would copy from the control and paste into a text editing application that doesn’t support formatting commands (for example, Notepad).

The RTF Code

H you click the Show RTF button, you’ll see the actual RTF code that produced. the formatted document in Figure 9.3. The message box with the RTF code is shown in Figure 9.4. This is all the information the RichTextBox control requires to render the document. As complicated as it may look, it isn’t difficult to produce. In programming the RichTextBoxcontrol, you’ll rarely have to worry about inserting actual RTF tags in the code. The control is responsible for generating the RTF code and for rendering the document, You simply manipulate a few properties (the recurring theme in VisualBasic programming) and the control does the rest.



On rather rare occasions, you may have to supply RTFtags. You’ll see an example of this in Chapter 11, Recursive Programming, with the DirMap application, which produces RTFcode on the fly.You don’t have to know much about RTFtags, though. Simply format a few words with the desired attributes using the RTFDemo application (or experiment with the Immediate window), copy the tags that produce the desired result, and use them in your application. If you are curious about RTF, experiment with the RTFDemo application.

One of the most interesting applications on the book’s CD-ROM is the RTFPad application, a word processing application that’s discussed in detail later in this chapter. This application duplicates much of the functionality of Windows Word- Pad, but it’s included in this book to show you how the RichTextBox control is used. The RTFPad applicationc an become your starting point for writing custom word processing applications (a programmer’s text editor with color-coded keywords, for example).

Text Manipulation Properties

The RichTextBox control’s properties for manipulating selected text start with the prefix Sel. The most commonly used properties related to the selected text are shown in Table 9.1.



SelText, for example, represents the selected text. To assign the selected text to a variable. use the following statement:


RichTextbox1 is the name of the control. You can also modify the selected text by assigning a new value to the SeIText property. The following statement converts the selected text to uppercase:


If you assign a string to the SeIText property, the selected text in the control is replaced with the string. The following statement replaces the current selection. on the RichTextbox1 control with the string “Revised string”:

RichTextbox1.SelText = “Revised string”

If no text is selected, the statement inserts the string at the location of the pointer. It is possible, therefore, to insert text automatically by assigning a string to the Sel- Text property.

To simplify the manipulation and formatting of the text on the control, two additional properties, Se/Start and Se/Length, report the position of the first selected character in the text and the length of the selection, respectively. You can also set the values of these properties to select a piece of text from within your code. One obvious use of these properties is to select (and highlight) the entire text (or a segment of the text):

RichTextBox1.SelStart= 0

Sellndent, SelRightIndent, and SelHanginglndent allow you to manipulate the indentation of the text. These properties apply to the selected text only. The text is aligned with the left side of the control. However, if you set the Sellndent property while the control is still empty, every time the user presses Enter, new paragraphs inherit the same left margin setting.

Posted on November 5, 2015 in More Advanced ActiveX Controls

Share the Story

Back to Top
Share This