TELLING IT LIKE IT IS WITH THE MICROSOFT RICH TEXTBOX CONTROL

Access text boxes are pretty handy to use as far as inputting data for storage and display. With the capability to specify scroll bars and use the CanGrow/CanShrink properties, they're as versatile as most other DBMSs in the market today. However, with the growing technology of Windows and developers giving users more WYSIWYG (“what you see is what you get”), the plain ol' text box just doesn't cut it sometimes.

The ActiveX Microsoft Rich Textbox control gives you another tool for enriching your interfaces. With the Rich Textbox control, you can

  • Set fonts and font sizes

  • Select text inside the Rich Textbox control

  • Specify special effects, such as boldface and italic

  • Load and save text from or to files

  • Use bullets

  • Convert text to or from Rich Text Format

  • Set the alignment for paragraphs

The WindowsCommonControlsRichText form has an example that allows users to load and save text from a Rich Textbox control. You also can select text inside the control and then toggle the text to be boldface or italic, and choose the alignment you desire. The last thing you can do with this example is to print the text.

Figure 14.17 shows the WindowsCommonControlsRichText form as a subform on WindowsCommonControls.

Figure 14.17. The Microsoft Rich Textbox control offers an abundance of features for formatting and presenting text.


Properties of the Rich Textbox Control

The code behind each button on the WindowsCommonControlsRichText form is examined in a moment. First, look at some of the properties of the Rich Textbox control that affect the selected text (see Table 14.4).

Table 14.4. Properties of the Microsoft Rich Textbox Control
Property Description
SelAlignment[*] Affects the alignment of the selected text. Possibilities are 0 - Left, 1 - Right, and 2 - Center. Also returns the current setting.
SelBold[*] Boldfaces or unbolds text based on the value set (0 - Un-Bold or 1 - Bold). Also returns the current setting.
SelBullet Sets paragraphs with bullets, indented by whatever the BulletIndent property is set (0 - No Bullets, 1 - Bullets, or Null - Spans more than one paragraph). Also returns the current setting.
SelColor[*] Sets the foreground color of text. May be set by using RGB() or the CommonDialog ActiveX control. Also returns the current setting.
SelFontName Specifies the font name for the selected text. Can be set by supplying the name or by using the CommonDialog ActiveX control. Also returns the current setting.
SelFontSize Specifies the font size for the selected text. Can be set by supplying the size or by using the CommonDialog ActiveX control. Also returns the current setting.
SelHangingIndent Used with SelIndent and SelRightIndent to specify the indentation for a paragraph. Also returns the current setting.
SelIndent Used with SelHangingIndent and SelRightIndent to specify the indentation for a paragraph. Also returns the current setting.
SelItalic[*] Applies or removes italics in text based on the value set (0 - Unset Italics, 1 - Italics). Also returns the current setting.
SelLength Set the amount of text to currently select. Used with SelStart. Also returns the current setting.
SelRightIndent Used with SelHangingIndent and SelIndent to specify the indentation for a paragraph. Also returns the current setting.
SelRTF Converts the selected text into Rich Text Format.
SelStart Specifies the starting position of the selected text. If no code is selected, it gives the position of the cursor.
SelStrikethru Applies or removes strikethrough text based on the value set (0 - Unset Strikethru or 1 - Strikethru). Also returns the current setting.
SelText Returns the currently selected text.
SelUnderline[*] Applies or removes underline text based on the value set (0 - Unset Underline, or 1 - Underline). Also returns the current setting.

[*] Example found on the WindowsCommonControlsRichText form

Code Behind the Microsoft Rich Textbox Control

As with other properties, you can tell what the current status of each property is by simply viewing the property. On the WindowsCommonControlsRichText form, the cmdBold and cmdItalics buttons toggle the SelBold and SelItalic properties. Listings 14.8 and 14.9 show the code for each button. First, Listing 14.8 shows the cmdBold_Click code.

Listing 14.8. Chap14.mdb: Changing Selected Text to Bold
Private Sub cmdBold_Click()

   On Error GoTo Error_cmdBold_Click

   '-- Toggle Bold
   Me!ocxRichText.SelBold = Not Me!ocxRichText.SelBold

Exit_cmdBold_Click:
   Exit Sub

Error_cmdBold_Click:
   MsgBox Error$
   Resume Exit_cmdBold_Click

End Sub

Listing 14.9 shows the cmdItalics OnClick code.

Listing 14.9. Chap14.mdb: Changing Selected Text to Italics
Private Sub cmdItalics_Click()

   On Error GoTo Error_cmdItalics_Click
   '-- Toggle Italics
   Me!ocxRichText.SelItalic = Not Me!ocxRichText.SelItalic

Exit_cmdItalics_Click:
   Exit Sub

Error_cmdItalics_Click:
   MsgBox Error$
   Resume Exit_cmdItalics_Click

End Sub

Setting the alignment takes a little bit more work, but not much. Because the SelAlignment property takes one of three values, a combo box has been set up on the form to reflect these choices. Here is the RowSource for the cboAlignment combo box, which is a Value list:

0;"Left";1;"Right";2;"Center"

The AfterUpdate event contains the event procedure that actually sets the SelAlignment property (see Listing 14.10).

Listing 14.10. Chap14.mdb: Changing Selected Text Alignment
Private Sub cboAlignment_AfterUpdate()

   On Error GoTo Error_cboAlignment_AfterUpdate

   '-- Set the alignment
   Me!ocxRichText.SelAlignment = Me!cboAlignment

Exit_cboAlignment_AfterUpdate:
   Exit Sub

Error_cboAlignment_AfterUpdate:
   MsgBox Error$
   Resume Exit_cboAlignment_AfterUpdate

End Sub
						

The last two pieces of code to look at for the Rich Textbox control are for loading from and saving to files, using the text in the control. Listing 14.11 shows the cmdLoad_Click subroutine, which is attached to the OnClick event of the cmdLoad command button.

Listing 14.11. Chap14.mdb: Loading a File into the Control
Private Sub cmdLoad_Click()

   On Error GoTo Error_cmdLoad_Click

   Me!ocxCommon.Filter = "Rich Text Format files|*.rtf"
   Me!ocxCommon.ShowOpen
   Me!ocxRichText.LoadFile Me!ocxCommon.Filename, 0

Exit_cmdLoad_Click:
   Exit Sub

Error_cmdLoad_Click:
   MsgBox Error$
   Resume Exit_cmdLoad_Click

End Sub

This routine uses a CommonDialog control, using the ShowOpen method, to get the file to open. The routine to save the text, shown in Listing 14.12, is similar to the load routine and can be found on the OnClick event of the cmdSave command button.

Listing 14.12. Chap14.mdb: Saving a File from the Control
Private Sub cmdSaveFile_Click()

   On Error GoTo Error_cmdSaveFile_Click

   Me!ocxCommon.ShowSave
   Me!ocxRichText.SaveFile Me!ocxCommon.Filename, 0

Exit_cmdSaveFile_Click:
   Exit Sub

Error_cmdSaveFile_Click:
   MsgBox Error$
   Resume Exit_cmdSaveFile_Click

End Sub

You can use the Microsoft Rich Textbox control in a lot more tasks just by expanding on the WindowsCommonControlsRichText form.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.27.45