![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
Loading Data AsynchronouslyWhen an HTML document creates an ActiveX control using the OBJECT statement, it can use PARAM clauses to specify the controls properties. Script code can also set control properties. These methods are fine for setting control properties that are integers, strings, Booleans, and other simple data types. They do not work, however, for pictures. An HTML document cannot set the VALUE field in a PARAM statement to point to a picture file. Even if this was possible, this sort of property assignment would violate one of the goals of HTML. HTML documents are designed to be loaded in pieces asynchronously. A document may contain references to pictures and other resources located on the Web. After the main document has been loaded, the browser loads the references and places them where they belong in the document. This allows the user to see the bulk of the document while larger items such as pictures are downloaded. Setting a controls picture property using a PARAM statement would slow the entire process. The browser would be forced to wait until the picture was loaded before it could continue displaying the main document. Loading Asynchronous DataActiveX controls can provide their own asynchronous features using the UserControl objects Async-Read subroutine. AsyncRead takes three parameters. The first is the location of the resource to be loaded. This can be a path if the data is located on the local computer, or it can be a URL (Uniform Resource Locator) if the data should be loaded across the network. AsyncReads second parameter indicates how the data should be returned to the ActiveX control. This parameter can have one of the following values:
AsyncReads final parameter is the name of the object being loaded asynchronously. This can be any string value defined by the control code. When the data has been loaded, the UserControl objects AsyncReadComplete event handler is executed. This routine receives as a parameter an object of type AsyncProperty. This object contains three properties of its own: AsyncType, PropertyName, and Value. AsyncType and PropertyName are the same as the second and third parameters passed to the AsyncRead method. PropertyName is particularly useful if the control loads several data items asynchronously. The event handler can check the value of PropertyName to determine which value has been returned. The AsyncProperty objects Value property is a variant containing the actual data. Loading Pictures on the WebThe gauge control in the Ch15\Combined subdirectory defines five different styles of gauge control. A gauge with the picture style displays its values by filling its control area with a picture. The picture is specified by the controls FillPicture property. As is mentioned in the previous section, HTML code cannot set the values of picture properties directly. To allow HTML code to specify a fill picture, the control can provide an alternative property named FillPictureURL. This property is initialized, saved, and retrieved just like any other string property by the UserControls InitProperties, ReadProperties, and WriteProperties event handlers. The FillPictureURL property let procedure stores the propertys new value. It then checks to see if the control is running in user mode rather than in design mode. If it is, and if the FillPictureURL property is nonblank, the control uses the AsyncRead method to begin reading the picture specified by the property value.
Public Property Let FillPictureURL(url As String)
fill_picture_url = url
If its run time start the read.
If Ambient.UserMode And url <> Then
AsyncRead url, vbAsyncTypePicture, _
FillPictureURL
End If
PropertyChanged FillPictureURL Tell VB.
The gauge is repainted when the read
finshes.
End Property
When the picture has been loaded, the AsyncReadComplete event handler executes. This routine checks the name of the returned data. If the name is FillPictureURL, the routine sets the FillPicture property to the data retrieved. AsyncReadComplete uses the On Error GoTo statement to protect itself against errors. There are many ways in which an error can occur while downloading data across a network. All AsyncReadComplete event handlers must be prepared to handle errors gracefully.
Private Sub UserControl_AsyncReadComplete(AsyncProp As AsyncProperty)
On Error GoTo AsyncReadError
If AsyncProp.PropertyName = FillPictureURL Then
Set FillPicture = AsyncProp.Value
End If
AsyncReadError:
End Sub
The HTML code in Gauge.HTM specifies the picture gauges FillPictureURL property using the following PARAM statement. The control asynchronously loads the bitmap file Gauge/Smile.bmp and uses it for the FillPicture property. <PARAM NAME=FillPictureURL VALUE=Gauge/Smile.bmp> SummaryAdding an ActiveX control to a Web page is relatively easy using the Setup Kit Wizard. The Wizard builds the controls .OCX and cabinet files, and creates a small sample Web document that invokes the control. This document includes a reference to the controls class ID. Using this ID, it is easy to use the HTML OBJECT statement to add new instances of the control to other Web pages. ActiveX controls are extremely flexible. They can contain standard controls as well as other ActiveX controls. In fact, an ActiveX control that contains enough constituent controls can practically implement a complete application. Placing such a control on a Web page allows a designer to bring nearly all of the features of Visual Basic to the Web.
|
|
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.
|