VB6 at Work: The TxtMargin Project

To see the Slider control in use, let’s review a segment of another application, the RTFPad application, which is covered in.Chapter 8, Advanced ActiveX Conirols. The/ Form ‘shown in Figure 5.18 contains a RichTextBox control and two sliders. The RichTextBox control will be explained in Chapter 9. All you need to know about the control to follow the code is that the RichTextBox control is similar to a TextBox control, but provides many more editing and formatting options. Two of the control’s properties we’ll use in this example are the Sellndent andSell-Ianginglndent properties and their functions are as follows:
• SelIndent Specifies the amount by which the currently selected paragraph(s) is indented from the left side of the control
• SelHanginglndent Specifies the amount of the hanging indentation (that is, the indentation of all paragraph lines after the first line)

The two sliders above the RichTextBox control let the user manipulate these . two indentations. Because each paragraph in a RichTextBox control is a separate entity, it can be formatteddifferently. The upper slider controls the paragraph’s indentation, and the lower slider controls the paragraph’s hanging indentation. You can open the TxtMargin application in this chapter’s folder on the CD and check it out. Enter a few paragraphs of text and experiment with it to see how the sliders control the appearance of the paragraphs.

To create the Form shown in Figure 3.18, the left edge of the RichTextBox control must be perfectly aligned with th« sliders’ indicators at their leftmost position. When both sliders are at the tar left, the SelIndent and SelHangingIndent properties are zero. As each slider’s indicator is scrolled, these two properties change value, and the text is reformatted instal1t1y.All the action takes place in the Slider controls’ Scroll event. Here’s the code of the”Slider1 control, which controls the paragraph’s indentation:

The paragraph’s hanging indentation is not the distance of the text from the left edge of the control, but the distance of the paragraph from the leftmost character of the first line. That’s why every time the paragraph’s indentation changes, the program calls the Scroll event of the second slider to adjust the hanging indentation, even though the second slider hasn’t been moved. The hanging indentation is expressed as a percentage, and we get the ratio of the difference between the two controls and their maximum value. This difference can become negative too, in which case the hanging indentation is to the left of the normal indentation. Remove the following line from the Slider1 control’s Scroll event and see ,what happens:

Since the slider’s tick marks don’t correspond to physical units, we set the text’s indentation to a percentage of its total width, which is given by the RightMargin property of the RichTextBox control. The RightMargin property is the distance of the text’s right edge from the left side of the control. This percentage is the same as the percentage of the slider’s value divided by its maximum value. As soon as the SelIndent property is set, the entire paragraph is indented accordlrigly, The program then calls the second slider’s Scroll event, which adjusts the hanging indentation. The code for this event is shown here:

The numbers (which don’t correspond to any real units, but you could easily map them to inches, centimeters, or any other unit) are Label controls, carefully placed on the Form. The length of the two Slider controls is 7.5 inches (on a monitor with 92 dpi) and the Labels above the tick marks are arranged from within the Form’s Load event. The length of both Slider controls is 11,360twips, which is slightly larger than 7.5 inches. On a 92 dpi monitor, 7.5 inches correspond to 10,800 twiEs. This value is returned by the function

ScaleX 7.5, vblnches, vbTwips

This value is increased by 28 pixels to account for the fact that the control’s Width property is larger than the width of the slider (the carved line that runs across the control). Ideally, the $lider control should provide a property to return -the width of the actual slider, excluding the left and right margins. Since you don’t know the size of the margins, sizing a Slider control precisely is not a simple task. After setting the Slider control’s Width property, you must also reposition and resize the RichTextBox on the Form, so that its two margins coincide with the minimum and maximum values of the Slider control, respectively. By ‘default.fhe dimensionsof the various controls on a Form are returned by the Width and Height properties in twips. To convert twips to pixels, you must first find out how many twips correspond to a pixel on the specific monitor resolution with the $creen.TwipsPerPixeIX and Screen.TwipsPerPixelY properties and then use them in your code.
The Labels above the control’s tick marks are placed .there from within the Form’s Load event with the following code. The Labels aremembers of the Margin control array (Margin(O) being the Label that displays 0, Margin(l) the Label that displays 0.5, and so on). The first Label, Margin(O), must be placed over the first tick manually

Scroll to Top