The new vbAccelerator Site - more VB and .NET Code and Controls
Source Code
1 ActiveX Controls &nbsp
&nbsp

Coding With The vbAccelerator RichEdit Control

Coding Tips and Techniques for working with the vbAccelerator RichEdit control

This sample is here to give some general hints and tips for working with the vbAccelerator RichEdit control. Tips are divided into five sections:

  1. RichEdit Contents: Loading/Saving with Files and Strings
  2. Using Accelerator/Shortcut Keys
  3. Common Editor Operations
  4. Formatting
  5. Automatic Hyperlink Detection and Adding Hyperlinks
  6. Transparency and Background Pictures
Contents: Loading/Saving with Files and Strings
There are five methods and properties which affect the contents of the RichEdit control:
  • Contents
    Gets or sets the entire contents of the control to a string, in either RTF or Text format.
  • InsertContents
    Inserts RTF or Text from a string to replace the current selection.
  • SelectedContents
    Gets the currently selected content to a string in RTF or Text format.
  • LoadFromFile
    Loads a file into the RichEdit control, in either RTF or Text format.
  • SaveToFile
    Saves the contents of the RichEdit control to a file, in either RTF or Text format.
Note that the maximum length of a document by default is 32k. Loading a document longer than this will result in truncation. If you need a longer document, you can set it to any length you like (up to &H7FFFFFFF, which is 2Gb) using the MaxLength property.

Accelerator/Shortcut Keys
By default the control will automatically respond to shortcut keys such as Ctrl+C for Copy. If you are creating an application with a menu, you might want to override this behaviour and take control of the shortcut keys yourself. Each shortcut can be enabled or disabled using the AllowShortCut method.

You can also control whether the key accepts the tab key or not, regardless of the TabStop properties of other controls on the form using the TrapTab property. Note that Shift+Tab always tabs out of the control.

Common Editor Operations
The control has all the common editor operations and properties built in. These commands and properties relate to common editing actions:
  • Undo method, CanUndo property, UndoType property. Default shortcut Ctrl+Z
  • Redo method, CanRedo property, RedoType property. Default shortcut Ctrl+Y
  • Cut method, CanCut property. Default shortcut Ctrl+X
  • Copy method, CanCopy property. Default shortcut Ctrl+C
  • Paste method, CanPaste property. Default shortcut Ctrl+V
  • SelectAll method. Default shortcut Ctrl+A
And these properties relate to standard editor status displays:
  • CurrentLine
  • CharacterCount
  • LineCount


Formatting
The control provides control for numerous formatting properties:
  • Font Face and Style
    • Font
    • FontBold
    • FontItalic
    • FontUnderline
    • FontStrikeOut
    • FontSuperScript
    • FontSubScript
  • Colours
    • FontBackColour (RichEdit 2.0 or higher only)
    • FontColour
  • Other Selection Properties
    • FontLink (RichEdit 2.0 or higher only) - determines whether the selection acts as a hyperlink
    • FontProtected (RichEdit 2.0 or higher only) - sets whether the control raises a ModifyProtected event when the user attempts to modify the text. By default the control will stop the user modifying the text, but if you set the bDoIt parameter to True then the text can be modified.
Unlike the VB RichText control, which provides separate methods for setting formatting generally and for a selection, the vbAccelerator RichEdit control has a property which determines what scope the format will apply to.

The CharFormatRange property can be set to format everything, the current selection or the current word. The default setting is the current selection. To set the font going forward from the current position, use code like this:

Dim lS As Long, lE As Long

With redMain
.GetSelection lS, lE
.SetSelection lS, lS
.FontBold = True
End With


Automatic Hyperlink Detection and Adding Hyperlinks
The automatic hyperlink detection feature, provided with RichEdit version 2.0, is a built-in feature from Microsoft. It is important to note that this does not provide the quite the same functionality as their newer products (for example Outlook Express 5.0). Basically, the code detects hyperlinks on the basis of one these signatures:

  • http:
  • file:
  • mailto:
  • ftp:
  • https:
  • gopher:
  • nntp:
  • prospero:
  • telnet:
  • news:
  • wais:
Therefore you cannot just type in (for example) www.vbaccelerator.com and get it to underline; the prefix needs to be included as well. A future vbAccelerator sample will demonstrate how to detect these types of addresses too.

To enable automatic hyperlink detection in the control, use the AutoURLDetect property.

You can create your own hyperlinks regardless of the automatic detection provided by the control. Simply set the CharFormatRange property to selection, select the word or words you want to form a hyperlink and then set the FontLink property. Note that when the user clicks the word, the RichEdit control will only pass the position of the clicked word to you, so you may need to internally store more information about the link yourself.

The VB/VBA Object Browser uses a RichEdit control in this way:

RichEdit 2.0 Control Used in VB's Object Browser



Transparency and Background Pictures
The vbAccelerator RichEdit control supports transparency, however due to limitations in the way VB draws controls the transparency only goes as far as the control itself. There is a built-in method to tile a picture onto the background of the control. To use this, set both the Transparent and the Picture property.

If you want more sophisticated background pictures, take the source code and modify the pDrawBackground sub in the vbalRichEdit code (and don't forget to change the filename and ProgId when you recompile it!)




TopBack to top
Source Code - What We're About!Back to RichEdit Control
Source Code - What We're About!Back to ActiveX Control Source Code

AboutContributeSend FeedbackPrivacy

Copyright 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 15 November 1999