![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
SPRY Mosaic version 03.0A.08.19, however, displays all of the VBScript code as if it were normal output text. To reduce the amount of garbage output, Histgram.HTM begins its VBScript code with the normal HTML comment delimiter <!--. This command is not recognized by VBScript so the document comments this delimiter out using the standard VBScript comment character (apostrophe). The end of the script code includes the closing HTML comment delimiter --> again commented out by the VBScript comment character. With this arrangement, VBScript-enabled browsers execute the VBScript code as usual. Netscape Navigator ignores all of the VBScript code including the comment delimiters. SPRY Mosaic displays an apostrophe but then ignores the bulk of the script code because it lies between the <!-- and the --> delimiters. Within the VBScript code, the Window_OnLoad routine initializes the histogram control. This routine is executed by the browser after the window has loaded.
<HTML>
<OBJECT ID=Histogram1 WIDTH=200 HEIGHT=184
CLASSID=CLSID:75CDDDBA-9A2A-11D0-AA34-0000E8167669
CODEBASE=http://beauty/histgram/Histgram.CAB#version=1,0,0,0>
<PARAM NAME=BarForeColor VALUE=16711680>
</OBJECT>
<SCRIPT LANGUAGE=VBScript>
<!--
Sub Window_OnLoad
Dim pi
Dim i
Histogram1.NumBars = 10
Histogram1.NumValues = 100
pi = 3.14159265
For i = 1 to 100
Histogram1.Value(CInt(i)) = CLng(50 + 50 * _
Sin(PI * i / 10) * _
Sin(PI * i / 15) * _
Cos(PI * i / 5))
Next
End Sub
-->
</SCRIPT>
</HTML>
Updating LabelsVBScript allows a Web page to perform all sorts of calculations. The example in the previous section uses VBScript to initialize the data displayed by a histogram control. VBScript also allows a Web page to respond to events generated by the controls it contains. These events have names very similar to those used by Visual Basic. For example, the Scroll event generated by a bar gauge control named Gauge1 would be called Gauge1_Scroll. The following code shows how a Web page can use VBScript to respond to this event.
<HTML>
<!-- Create controls with OBJECT statements, etc. -->
:
<SCRIPT LANGUAGE=VBScript>
<!--
Sub Gauge1_Scroll
Do something here.
End Sub
-->
</SCRIPT>
</HTML>
One of the more obvious actions to take in this sort of event handler would be to update a label control to reflect the gauges newly selected value. Unfortunately, HTML was not designed with updateable labels in mind. HTML was originally intended to allow the downloading of static documents. If something on the document needed to be updated, a revised version of the document would be downloaded. Fortunately, creating an ActiveX label control is easy. The Ch17\WebLabel subdirectory on the compact disk defines a simple label control suitable for use on Web pages. It uses two Visual Basic label controls, one with Appearance property set to 0 - Flat and the other set to 1 - 3D. The control uses these labels to display itself with either a two- or three-dimensional appearance. The control also supports BackColor, BorderStyle, ForeColor, and Caption properties. It delegates these properties to the label controls. The HTML code contained in Gauge.HTM in the Ch17 subdirectory uses WebLabel controls to respond to events generated by gauge controls. The code creates five gauge controlsone of each of the styles: bar gauge, dial, picture gauge, tic gauge, and wide gauge. For each gauge the document creates a WebLabel control. VBScript code updates the labels when the corresponding gauges Scroll and Change events occur. The following code fragment shows how Gauge.HTM handles the bar gauge control and its associated label:
<HTML>
<!-- Create the bar gauge -->
<OBJECT ID=Gauge0 WIDTH=126 HEIGHT=18
CLASSID=CLSID:1AC27B55-9A55-11D0-AA35-0000E8167669
CODEBASE=http://beauty/Gauge/Gauge.CAB#version=1,0,0,0>
<PARAM NAME=ForeColor VALUE=16711680>
<PARAM NAME=BackColor VALUE=8454016>
<PARAM NAME=Value VALUE=10>
<PARAM NAME=Style VALUE=0>
</OBJECT>
<!-- Create the bar gauges label -->
<OBJECT ID=WebLabel0 WIDTH=50 HEIGHT=18
CLASSID=CLSID:1AC27CA9-9A55-11D0-AA35-0000E8167669
CODEBASE=http://beauty/WebLabel/WebLabel.CAB#version=1,0,0,0>
<PARAM NAME=ForeColor VALUE=0>
</OBJECT>
<BR>
:
<!-- Create the other gauges and their labels -->
:
<SCRIPT LANGUAGE=VBScript>
<!--
Bar gauge events
Sub Gauge0_Scroll
WebLabel0.Caption = Gauge0.Value
End Sub
Sub Gauge0_Change
WebLabel0.Caption = Gauge0.Value
End Sub
:
Events for the other gauges.
:
Set the initial label values when we load.
Sub Window_OnLoad
Gauge0_Change
Gauge1_Change
Gauge2_Change
Gauge3_Change
Gauge4_Change
End Sub
-->
</SCRIPT>
</HTML>
Selecting ColorsWhen the controls in the previous examples are created, PARAM statements specify their foreground and background colors. For instance, colors for the bar gauge created in Gauge.HTM are set by the following lines. <PARAM NAME=ForeColor VALUE=16711680> <PARAM NAME=BackColor VALUE=8454016> The values 16711680 and 8454016 are the long integer representations for the colors blue and light green, respectively. You can calculate other color values by writing a Visual Basic program to display them. You can also execute statements such as ?RGB(0, 0, 255) in Visual Basics Immediate Window to calculate color values.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|