Monday, February 7, 2011

Lesson 3-Objects and VBScript

Objects, both in the form of Java applets and ActiveX controls, enhance the functionality that is provided with HTML. By using VBScript you can extend the capabilities of these controls, integrating and manipulating them from within your scripts. In this lesson we will look at how you can utilize the power of objects with VBScript.
Scripting with objects involves two steps:
  • Adding the object to your web page using HTML
  • Writing script procedures to respond to events that the object provides

Adding Objects to Your Web Pages

Since this is a VBScript tutorial, rather than an HTML tutorial, we will offer only a limited discussion of how to add an object to a web page. Objects, whether they're Java applets or ActiveX controls are added to a page with the <OBJECT> tag. The properties, or characteristics, of the object are configured using the <PARAM> tag. Typically you will see an object implemented using a single <OBJECT> tag along with several <PARAM> tags. The following HTML code demonstrates how an ActiveX control might appear when added to a page:
<OBJECT ID="lblTotalPay" WIDTH=45 HEIGHT=24 
CLASSID="CLSID:978C9E23-D4B0-11CE-BF2D-00AA003F40D0">
<PARAM NAME="ForeColor" VALUE="0">
<PARAM NAME="BackColor" VALUE="16777215">
<PARAM NAME="Caption" VALUE="">
<PARAM NAME="Size" VALUE="1582;635">
<PARAM NAME="SpecialEffect" VALUE="2">
<PARAM NAME="FontHeight" VALUE="200">
<PARAM NAME="FontCharSet" VALUE="0">
<PARAM NAME="FontPitchAndFamily" VALUE="2">
<PARAM NAME="FontWeight" VALUE="0">

Linking VBScript with Objects

Once you have added a control to your web page, it can be configured, manipulated and responded to through its properties, methods and events. Properties are the characteristics of an object. They include items like a caption, the foreground color and the font size. Methods cause an object to perform a task. Events are actions that are recognized by an object. For instance, a command button recognizes an onclick event.
Note
The Script Wizard found in the Microsoft ActiveX Control Pad can be used to identify events provided by a control, and to generate script to respond to these events.
For the most part, you will be focusing on properties and events. An example of setting properties for a label control is shown in the following example.
<SCRIPT LANGUAGE="VBScript">
Sub cmdCalculatePay_onClick
  Dim HoursWorked
  Dim PayRate
  Dim TotalPay
  HoursWorked = InputBox("Enter hours worked: ")
  PayRate = InputBox("Enter pay rate: ")
  TotalPay = HoursWorked * PayRate
  lblTotalPay.caption = TotalPay
End Sub
</SCRIPT>
The caption property of the label control, lblTotalPay, is set equal to the results of our calculation with the script line:
document.frmPayrate.lblTotalPay.caption = TotalPay


No comments:

Post a Comment