Shapouri & Associates

 

RTFEdit ActiveX

32 Bit OCX Control

Technical Document

Version 3.0
Designed & Developed by : Shahram Shapouri
 
 
 
Events . Methods . Properties
 
 
 
 
 For technical support please contact : Shahram Shapouri
Rich Text Box is an ActiveX control by Microsoft  (c) corporation.


 
 
 
 
 

 
 
 
 
 
 
 

Events

Click Event

MouseDown, MouseUp Events

MouseMove Event

DblClick Event

KeyDown, KeyUp Events

KeyPress Event

SelChange Event

 
 

Methods

EnableButton Method

ContextMenu Method

SaveFile Method

SelPrint Method

ShowToolbar

LoadFile Method

SaveFile Method

VisibleButton Method

 

Properties

BackColor Property

DataSource Property

DataField Property

SelAlignment Property

SelBold, SelItalic, SelStrikethru, SelUnderline Properties

SelBullet Property

SelColor Property

SelFontName , SelFontSize Property

SelLength, SelStart, SelText Properties

SelRTF Property

Text Property

ToolTipText Property

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 


Click Event

Occurs when the user presses and then releases a mouse button over editor's area.

Example :
Private Sub RTFEdit1_Click()
        MsgBox "Click event"
End Sub


MouseDown, MouseUp Events

Occur when the user presses (MouseDown) or releases (MouseUp) a mouse button.

Example :
Private Sub RTFEdit1_MouseUp (Button As Integer, Shift As Integer, X As Single, Y As Single)
        MsgBox "Mouse Up"
End Sub


MouseMove Event

Occurs when the user moves the mouse.

Example :
Private Sub RTFEdit1_MouseMove (Button As Integer, Shift As Integer, X As Single, Y As Single)
        MsgBox "Mouse Move"
End Sub


DblClick Event

Occurs when the user presses and releases a mouse button and then presses and releases it again over an object.

Example :
Private Sub RTFEdit1_DblClick()
        MsgBox "Double Click event"
End Sub


KeyDown, KeyUp Events

Occur when the user presses (KeyDown) or releases (KeyUp) a key while an object has the focus. (To interpret ANSI characters, use the KeyPress event.)

Example :
Private Sub RTFEdit1_KeyDown (KeyCode As Integer, Shift As Integer)
        MsgBox KeyCode & " Pressed"
End Sub


KeyPress Event

Occurs when the user presses and releases an ANSI key.

Example :
Private Sub RTFEdit1_KeyPress (KeyCode As Integer, Shift As Integer)
        MsgBox KeyCode & " Pressed"
End Sub


SelChange Event

Occurs when the current selection of text in the RTFEdit control has changed or the insertion point has moved.
You can use the SelChange event to check the various properties that give information about the current selection (such as SelBold).

Example :
Private Sub RTFEdit1_SelChange()
        If RTFEit1.SelBold Then
                MsgBox "The Current Style is BOLD"
        End If
End Sub


SaveFile Method

Saves the contents of a RTFEdit control to a file.

Syntax :
object.SaveFile(pathname, filetype)

The SaveFile method syntax has these parts:
 

Settings :
The settings for filetype are:
 
Constant 
Value 
 Description 
rtfRTF  0 (Default) RTF.  The RTFEdit control saves its contents as an .RTF file. 
rtfText  1 Text.  The RTFEdit control saves its contents as a text file. 
Example :
This example displays a dialog box to choose an .RTF file to which you will save the contents of a RTFEdit control. To try this example, put a RTFEdit control, a CommandButton control, and a CommonDialog control on a form. Paste this code into the Click event of the CommandButton control. Then run the example.

Private Sub Command1_Click()
        CommonDialog1.ShowSave
        RTFEdit1.SaveFile CommonDialog1.Filename, rtfRTF
End Sub


SelPrint Method

Sends formatted text in a RTFEdit control to a device for printing.

Syntax :
Object.SelPrint(hdc)

The SelPrint method syntax has these parts:
 
 
Part 
Description 
object  An object expression that evaluates to a RTFEdit control. 
hdc  The device context of the device you plan to use to print the contents of the control. 
Remarks :
If text is selected in the RTFEdit control, the SelPrint method sends only the selected text to the target device. If no text is selected, the entire contents of the RTFEdit are sent to the target device.
The SelPrint method does not print text from the RTFEdit control. Rather, it sends a copy of formatted text to a device which can print the text. For example, you can send the text to the Printer object using code as follows:

RTFEdit1.SelPrint(Printer.hDC)
 
Notice that the hDC property of the Printer object is used to specify the device context argument of the SelPrint method.
Note   If you use the Printer object as the destination of the text from the RTFEdit control, you must first initialize the device context of the Printer object by printing something like a zero-length string.

Example :
This example prints the formatted text in a RTFEdit control. To try this example, put a RTFEdit control, a CommonDialog control, and a CommandButton control on a form. Paste this code into the Click event of the CommandButton control. Then run the example.

Private Sub Command1_Click()
        CommonDialog1.Flags = cdlPDReturnDC + cdlPDNoPageNums
        If RTFEdit1.SelLength = 0 Then
                CommonDialog1.Flags = CommonDialog1.Flags + cdlPDAllPages
        Else
                CommonDialog1.Flags = CommonDialog1.Flags + cdlPDSelection
        End If
        CommonDialog1.ShowPrinter
        Printer.Print ""
        RTFEdit1.SelPrint CommonDialog1.hDC
End Sub


EnableButton Method

Enable or Disable a RTFEdit toolbar button.

Syntax :
RTFEdit.EnableButton IndexOfButton , StatusOfButton

Remarks :
Buttons numberd from 1 to 14. The font and fontsize combo boxes on the toolbar has same index of 14.

Example :
Private Sub Form_Load()
        RTFEdit1.EnabledButton 1 , False 'This will disable the color selection button
End Sub


ContextMenu Method

It causes to show or hide the RTFEdit context menu. (Popupmenu)

Syntax :
RTFEdit.ContextMenu StatusOfMenu

Example :
Private Sub Form_Load()
        RTFEdit1.ContextMenu  False 'This will disable the context menu
End Sub


ShowToolbar

It causes to show or hide the RTFEdit toolbar and margin bars. (Ruler)

Syntax :
RTFEdit.ShowToolbar StatusOfToolbar

Example :
Private Sub Form_Load()
        RTFEdit1.ShowToolbar  False 'Make toolbar and margin bars invisible
End Sub


LoadFile Method

Loads an .RTF file or text file into a RTFEdit control.

Syntax :
object.LoadFile pathname, filetype

The LoadFile method syntax has these parts:
 
Part 
Description
object Required. An object expression that evaluates to a RTFEdit Control.
pathname  Required. A string expression defining the path and filename of the file to load into the control.
filetype Optional. An integer or constant that specifies the type of file loaded, as described in Settings.
Settings :
The settings for filetype are:
 
Constant 
Value 
 Description
rtfRTF  0 (Default) RTF. The file loaded must be a valid .RTF file.
rtfText  1 Text. The RTFEdit control loads any text file.
Remarks :
When loading a file with the LoadFile method, the contents of the loaded file replaces the entire contents of the RTFEdit control. This will cause the values of the Text and RTFText properties to change.

Example :
This example displays a dialog box to choose an .RTF file, then loads that file into a RTFEdit control. To try this example, put a RTFEdit control, a CommandButton control, and a CommonDialog control on a form. Paste this code into the General Declarations section of the form. Then run the example.

Private Sub Command1_Click()
        CommonDialog1.Filter = "Rich Text Format files|*.rtf"
        CommonDialog1.ShowOpen
        RTFEdit1.LoadFile CommonDialog1.Filename, rtfRTF
End Sub


VisibleButton Method

Making a RTFEdit toolbar button Visible or Invisible.

Syntax :
RTFEdit.VisibleButton IndexOfButton , StatusOfButton

Remarks :
Buttons numberd from 1 to 14. The font and fontsize combo boxes on the toolbar has same index of 14.

Example :
Private Sub Form_Load()
        RTFEdit1.VisibleButton 1 , False 'This will make the color selection button invisible.
End Sub


BackColor Property

Returns or sets the background color of  RTFEdit control.

Syntax :
RTFEdit.BackColo [= VBColor]


SelAlignment Property

Returns or sets a value that controls the alignment of the paragraphs in a RTFEdit control. Not available at design time.

Settings :
The settings for value are:
 
 
Constant
Value
 Description
Null  Neither.   The current selection spans more than one paragraph with different alignments.
rtfLeft 0 (Default) Left. The paragraph is aligned along the left margin.
rtfRight 1 Right.  The paragraph is aligned along the right margin.
rtfCenter 2 Center. The paragraph is centered between the left and right margins.
Remarks :
The SelAlignment property determines paragraph alignment for all paragraphs that have text in the current selection or for the paragraph containing the insertion point if no text is selected.
To distinguish between the values of Null and 0 when reading this property at run time, use the IsNull function with the If...Then...Else statement. For example:

If IsNull(RTFEdit1.SelAlignment) = True Then
        ' Code to run when selection is mixed.
ElseIf RTFEdit1.SelAlignment = 0 Then
        ' Code to run when selection is left aligned.
End If

Example :
This example uses an array of OptionButton controls to change the paragraph alignment of selected text in a RTFEdit control, but only if text is selected. The indices of the controls in the array correspond to settings for the SelAlignment property. To try this example, put a RTFEdit control and three OptionButton controls on a form. Give all three of the OptionButton controls the same name and set their Index property to 0, 1, and 2. Paste this code into the Click event of the OptionButton control. Then run the example.

Private Sub Option1_Click(Index As Integer)
        If RTFEdit1.SelLength > 0 Then
                RTFEdit1.SelAlignment = Index
        End If
End Sub


SelBold, SelItalic, SelStrikethru, SelUnderline Properties

Return or set font styles of the currently selected text in a RTFEdit control. The font styles include the following formats: Bold, Italic, Strikethru, and Underline. Not available at design time.

Syntax :
RTFEdit.SelBold [= value]
RTFEdit.SelItalic [= value]
RTFEdit.SelStrikethru [= value]
RTFEdit.SelUnderline [= value]
[Value] is a Boolean expression or constant that determines the font style.

Example :
Private Sub Command1_Click()
           RTFEdit1.SelBold = True
End Sub


SelBullet Property

Returns or sets a value that determines if a paragraph in the RTFEdit control containing the current selection or insertion point has the bullet style. Not available at design time.

Syntax :
RTFEdit.SelBullet [= value]
[Value] is a Boolean expression or constant that determines the font style.

Example :
Private Sub Command1_Click()
           RTFEdit1.SelBullet = True
End Sub


SelColor Property

Returns or sets a value that determines the color of text in the RTFEdit control. Not available at design time.

Syntax :
RTFEdit.SelColor = [Color]
[Color] Is a value that specifies a color.

Example :
Private Sub Command1_Click()
           RTFEdit1.SelColor = VbRed
End Sub


SelFontName , SelFontSize Property

Returns or sets the font and fontsize used to display the currently selected text or the character(s) immediately following the insertion point in the RTFEdit control. Not available at design time.

Syntax :
RTFEdit.SelFontName = [String]
RTFEdit.SelFontSize = [Value]
[String] Is a windows installed font name.
[Value] Is font size in points.

Example :
Private Sub Command1_Click()
           RTFEdit1.SelFontName = "Arial"
           RTFEdit1.SelFontSize = 22
End Sub


SelLength, SelStart, SelText Properties

These properties aren't available at design time.
Syntax
object.SelLength [= number]
object.SelStart [= index]
object.SelText [= value]
 
The SelLength, SelStart, and SelText property syntaxes have these parts:
 
Part
 Description
object  An object expression that evaluates to a RTFEdit control.
number A numeric expression specifying the number of characters selected. For SelLength and SelStart, the valid range of settings is 0 to text length --- the total number of characters in the edit area RTFEdit control.
index  A numeric expression specifying the starting point of the selected text, as described in Settings.
value  A string expression containing the selected text.
Remarks :
Use these properties for tasks such as setting the insertion point, establishing an insertion range, selecting substrings in a control, or clearing text. Used in conjunction with the Clipboard object, these properties are useful for copy, cut, and paste operations.
When working with these properties: Example :
This example enables the user to specify some text to search for and then searches for the text and selects it, if found. To try this example, paste the code into the Declarations section of a form that contains a wide TextBox control, and then press F5 and click the form.

Private Sub Form_Load ()
        RTFEdit1.Text = "This is a sample text for using with RTFEdit ActiveX control"
End Sub

Private Sub Form_Click ()
Dim Search, Where ' Declare variables.
' Get search string from user.
Search = InputBox("Enter text to be found:")
Where = InStr(RTFEdit1.Text, Search) ' Find string in text.
        If Where Then ' If found,
                RTFEdit1.SelStart = Where - 1 ' set selection start and
                RTFEdit1.SelLength = Len(Search) ' set selection length.
        Else
                MsgBox "String not found." ' Notify user.
        End If
End Sub


SelRTF Property

Returns or sets the text (in .RTF format) in the current selection of a RTFEdit control. Not available at design time.

Syntax :
object.SelRTF [= string]

Remarks :
Setting the SelRTF property replaces any selected text in the RTFEdit control with the new string. This property returns a zero-length string ("") if no text is selected in the control.
You can use the SelRTF property along with the Print function to write .RTF files.

Example :
This example saves the highlighted contents of a RTFEdit control to an .RTF file. To try this example, put a RTFEdit control and a CommandButton control on a form. Paste this code into the Click event of the CommandButton control. Then run the example.

Private Sub Command1_Click ()
        Open "mytext.rtf" For Output As 1
        Print #1, RTFEdit1.SelRTF
        Close 1
End Sub


Text Property

Returns or sets the text contained in an object.

Syntax :
object.Text [= string]


ToolTipText Property

Returns or sets a ToolTip.

Syntax :
object.ToolTipText [= string]


DataSource Property

Sets a value that specifies the Data control through which the current RTFEdit control is bound to a database. Not available at run time.

Remarks:
To bind a RTFEdit control to a field in a database at run time, you must specify a Data control in the DataSource property at design time using the Properties window.
To complete the connection with a field in the Recordset managed by the Data control, you must also provide the name of a Field object in the DataField property. Unlike the DataField property, the DataSource property setting isn't available at run time.


DataField Property

Returns or sets a value that binds a control to a field in the current record

Syntax:
object.DataField [= value]

The DataField property syntax has these parts:
 
 
Part  Description
object An object expression that evaluates to a RTFEdit control.
value A string expression that evaluates to the name of one of the fields in the Recordset object specified by a Data control's RecordSource and DatabaseName properties.


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
This document  containing articales from  Microsoft's 'Rich Text Box' help file.
Copyright Micosoft Corporation.