Steve Kirk
Microsoft Developer Network Technology Group
July 27, 1996
Introduction
Creating Instances of the Objects
Creating the Script
Text Control Events, Methods, and Properties
This article explains how the ActiveX™ Text control can be used with the ActiveX Timer control and Microsoft® Visual Basic® Scripting Edition (VBScript) to create a continuously changing text message.
In this article, we'll explain how to cycle a text object, randomly, through a series of stored messages by implementing ActiveX objects and Visual Basic Scripting Edition. We will first create instances of the ActiveX Timer and Text objects in Hypertext Markup Language (HTML) and initialize the properties of the objects. We will then add VBScript code to the HTML page to store data and to provide methods for the ActiveX objects.
To complete this example, you will need the ActiveX Text and Timer controls, which can be found in the Microsoft ActiveX SDK (the ActiveX SDK can be downloaded from the SDK section of the MSDN Online Web site at http://www.microsoft.com/msdn/), and a scripting language such as Microsoft Visual Basic Scripting Edition or Netscape Communications JavaScript. In this example, we will be using Visual Basic Scripting Edition as our scripting language.
The following HTML code fragment creates the ActiveX Text control and initializes its properties. Using the HTML <OBJECT> tag, we define this object as txtFactoid and indicate that it will be an ActiveX Text control by specifying its class ID. (The class ID can be obtained from the registry or the type library of the control, or it can be generated by the ActiveX Control Pad, available from http://www.microsoft.com/workshop/author/cpad/.) We also set properties to determine the size and font of the Text control using the <PARAM> tag.
<OBJECT ID="txtFactoid" WIDTH=130 HEIGHT=130
CLASSID="CLSID:8BD21D10-EC42-11CE-9E0D-00AA006002F3">
<PARAM NAME="VariousPropertyBits" VALUE="-1400879085">
<PARAM NAME="ScrollBars" VALUE="2">
<PARAM NAME="Size" VALUE="6350;3457">
<PARAM NAME="SpecialEffect" VALUE="0">
<PARAM NAME="FontName" VALUE="Garamond">
<PARAM NAME="FontCharSet" VALUE="0">
<PARAM NAME="FontPitchAndFamily" VALUE="2">
<PARAM NAME="FontWeight" VALUE="0">
<PARAM NAME="FontHeight" VALUE="200">
</OBJECT>
The next piece of code is the <OBJECT> tag for the ActiveX Timer object. The key properties are the class ID, which identifies this object as an ActiveX Timer, the interval, which specifies how often (in milliseconds) the timer fires, and the object ID, which is used to associate this timer with the VBScript subroutine that is called each time the timer fires.
<OBJECT ID="tmrFactoid" WIDTH=0 HEIGHT=0
CLASSID="CLSID:59CCB4A0-727D-11CF-AC36-00AA00A47DD2">
<PARAM NAME="Interval" VALUE="2000">
</OBJECT>
(For more information on the ActiveX Timer control, see "Creating a Slide Show Using the ActiveX Timer Control".)
The following VBScript code is placed between the <SCRIPT> and </SCRIPT> tags on the HTML page. The first statement dimensions an array to hold the strings for the various titles. Strings are then assigned to the elements of the array:
<SCRIPT LANGUAGE="VBScript">
dim strFactoids(14)
strFactoids(0) = "Scientists have…"
'lines for elements (1) to (13) skipped here
strFactoids(14) = "Jojoba, a shrub related to boxwood…"
In Visual Basic Scripting Edition, the Web page is an implied object called Window, and one of its events is the Load event. The link between object events and the corresponding VBScript code is a naming convention that combines the object ID with the event, separated by an underscore. Following this convention, we can see that subroutine Window_Onload will be called when the Web page loads. Subroutine Window_OnLoad first calls Randomize, to initialize the seed number for the RND function, then calls SetFactoid to initialize txtFactoid:
sub Window_Onload()
'Init random number sequence
Randomize
'Set inital value of txtFactoid
Call SetFactoid
end sub
Subroutine tmrFactoid_Timer is called when the Timer event for the Timer object tmrFactoid occurs. Note that the interval property of tmrFactoid determines how often, in milliseconds, this event happens:
Sub tmrFactoid_Timer()
Call SetFactoid
end sub
The final subroutine, SetFactoid, picks one of the text strings stored in the strFactoid() array and uses it to set the value property of txtFactoid:
Sub SetFactoid()
txtFactoid.Locked = False
txtFactoid.Value = strFactoids(Int((UBound(strFactoids) + 1) * Rnd))
end sub
The tables below list the events, methods, and properties supported by the Text control. For a similar listing for the ActiveX Timer control, please see "Creating a Slide Show Using the ActiveX Timer Control".
Table 1. ActiveX Text Control Events
Event | Description |
AfterUpdate | Occurs after data in a control is changed through the user interface. |
BeforeDragOver | Occurs when a drag-and-drop operation is in progress. |
BeforeDropOrPaste | Occurs when the user is about to drop or paste data onto an object. |
BeforeUpdate | Occurs before data in a control is changed. |
Change | Occurs when the Value property changes. |
DblClick | Occurs when the user points to an object and then clicks a mouse button twice. |
Enter, Exit | Enter occurs before a control actually receives the focus from a control on the same HTML layout. Exit occurs immediately before a control loses the focus to another control on the same HTML layout. |
Error | Occurs when a control detects an error and cannot return the error information to a calling program. |
KeyDown, KeyUp | Occur in sequence when a user presses and releases a key. KeyDown occurs when the user presses a key. KeyUp occurs when the user releases a key. |
KeyPress | Occurs when the user presses an ANSI key. |
MouseDown, MouseUp | Occur when the user clicks a mouse button. MouseDown occurs when the user presses the mouse button; MouseUp occurs when the user releases the mouse button. |
MouseMove | Occurs when the user moves the mouse. |
Table 2. ActiveX Text Control Methods
Method | Description |
Copy | Copies the contents of the control to the Clipboard. |
Cut | Removes selected information from the control and transfers it to the Clipboard. |
GetFromClipboard, GetText | GetFromClipboard moves data from the Clipboard to the control. GetText retrieves a text string from the Clipboard using a specified format. |
Move | Moves the control. |
Paste | Transfers the contents of the Clipboard to the control. |
PutInClipboard | Moves data from the control to the Clipboard. |
SetFocus | Moves the focus to the control. |
StartDrag | Initiates a drag-and-drop operation for the control. |
Zorder | Places the control at the front or back of the z order. |
Table 3. ActiveX Text Control Properties
Property | Description |
AutoSize | Specifies whether the control automatically resizes to display its entire contents. |
AutoTab | Specifies whether an automatic tab occurs when a user enters the maximum allowable number of characters into the control. Values: True: Tab occurs. False: Tab does not occur (default). |
AutoWordSelect | Specifies whether a word or a character is the basic unit used to extend a selection. Values: True: Uses a word as the basic unit (default). False: Uses a character as the basic unit. |
BackColor | Specifies the background color of the control. |
BackStyle | Control background style. Constants, values, and descriptions: fmBackStyleTransparent, value: 0—The background is transparent. fmBackStyleOpaque, value: 1—The background is opaque (default). |
BorderColor | Specifies the color of the control’s border. |
BorderStyle | Specifies the type of border used by the control. Constants, values, and descriptions: fmBorderStyleNone, value: 0—The control has no visible border line. fmBorderStyleSingle, value: 1—The control has a single-line border (default). |
CodeBase | Specifies the URL of the control’s Component Object Model (COM) object. |
DragBehavior | Specifies whether the system enables the drag-and-drop feature for the control. |
Enabled | Specifies whether the control can receive the focus and respond to user-generated events. |
EnterFieldBehavior | Specifies the selection behavior when entering the control. Constants, values, and descriptions: fmEnterFieldBehaviorSelectAll, value: 0—Selects the entire contents of the edit region when entering the control (default). fmEnterFieldBehaviorRecallSelection, value: 1—Leaves the selection unchanged. Visually, this uses the selection that was in effect the last time the control was active. |
EnterKeyBehavior | Defines the effect of pressing enter. Values: True: Pressing enter creates a new line. False: Pressing enter moves the focus to the next object in the tab order (default). |
Font | Defines the characteristics of the text used by the control. |
ForeColor | Foreground color of the control. |
Height | Height, in points, of the control. |
HideSelection | Specifies whether selected text remains highlighted when the control does not have the focus. |
ID | The name of the control. |
IMEMode | Specifies the default run-time mode of the Input Method Editor (IME) for a control. This property applies only to applications written for the Far East and is ignored in other applications. Constants, values, and descriptions: fmIMEModeNoOp, value: 0—Does not control IME (default). fmIMEModeOn, value: 1—IME on. fmIMEModeOff, value: 2—IME off. English mode. fmIMEModeDisable, value: 3—IME off. User can't turn on IME by keyboard. fmIMEModeHiragana, value: 4—IME on with full-width Hiragana mode. fmIMEModeKatakanaDbl, value: 5—IME on with full-width Katakana mode. fmIMEModeKatakanaSng, value: 6—IME on with half-width Katakana mode. fmIMEModeAlphaDbl, value: 7—IME on with full-width alphanumeric mode. fmIMEModeAlphaSng, value: 8—IME on with half-width alphanumeric mode. |
IntegralHeight | Indicates whether the control displays full lines of text in a list or partial lines. |
Left | The distance between the control and the left or top edge of the HTML layout that contains it. |
Locked | Specifies whether text can be edited. |
MaxLength | Specifies the maximum number of characters a user can enter in the control. |
MouseIcon | Assigns a custom icon to the control. |
MousePointer | Specifies the type of pointer displayed when the user positions the mouse over the control. |
MultiLine | Specifies whether the control can accept and display multiple lines of text. |
PasswordChar | Specifies whether placeholder characters are displayed instead of the characters actually entered in control. |
ScrollBars | Specifies whether a control, form, or page has vertical scroll bars, horizontal scroll bars, or both. |
SelectionMargin | Specifies whether the user can select a line of text by clicking in the region to the left of the text. |
SpecialEffect | Specifies the visual appearance of control. Constants, values, and descriptions: fmSpecialEffectFlat, value: 0—Object appears flat, distinguished from the surrounding form by a border, a change of color, or both. Default for Image and Label, valid for all controls. fmSpecialEffectRaised, value: 1—Object has a highlight on the top and left and a shadow on the bottom and right. Not valid for check boxes or option buttons. fmSpecialEffectSunken, value: 2—Object has a shadow on the top and left and a highlight on the bottom and right. The control and its border appear to be carved into the form that contains them. Default for CheckBox and OptionButton, valid for all controls (default). fmSpecialEffectEtched, value: 3—Border appears to be carved around the edge of the control. Not valid for check boxes or option buttons. fmSpecialEffectBump, value: 6—Object has a ridge on the bottom and right and appears flat on the top and left. Not valid for check boxes or option buttons. |
TabIndex | Specifies the position of the control object in the HTML layout's tab order. |
TabKeyBehavior | Determines whether tabs are allowed in the edit region Values: True: Pressing TAB inserts a tab character in the edit region. False: Pressing TAB moves the focus to the next object in the tab order (default). |
TabStop | Indicates whether the control can receive focus when the user tabs to it. |
Text | The actual text value of the control. |
TextAlign | Specifies how text is aligned in the control. Constants, values, and descriptions: fmTextAlignLeft, value: 1—Aligns the first character of displayed text with the left edge of the control's display or edit area (default). fmTextAlignCenter, value: 2—Centers the text in the control's display or edit area. fmTextAlignRight, value: 3—Aligns the last character of displayed text with the right edge of the control's display |
Top | The distance between the control and the top edge of the HTML Layout that contains it. |
Value | The text content of the control. |
Visible | Specifies whether the control is visible or hidden. |
Width | Width, in points, of the control. |
WordWrap | Indicates whether the contents of the control automatically wrap at the end of a line. |