The Location object applies to the Wmdow and Frames objects and provides information about the window’s (or frame’s) current URL. You’ve already seen examples of the Location object, hut we haven’t looked at all its properties yet. Here are all the properties of the Location object. The Location object’s properties return information about the URL of the current document. By setting this object’s properties, you can navigate to another document.
href Thisproperty returns or sets the complete URL for the location to be loaded into the browser’s window. Use this property to connect to another location through your pt ,code. To display the current document’s URL, use a statement such as the following:
The Links Object
Another invisible object is the Links object, which represents a link in an HTML document and exposes properties through which you can find out the destination of the link. The number of hyperlinks in the current document is givenby the property Links. Length, and each hyperlink in the document is given by the Links array. The URL of the first hyperlink is links(O);links(1) is the URL of the second hyperlink, and so on up to links(Links.Length-l}.
The Links arrayreturns a Links object, which in turn provides information about a hyperlink’s attributes. The Links object has the properties.
Building Internet-Enabled Controls
With the domination of the Internet and the merging of the desktop and the Web demonstrated by the Active Desktop and the latest release of Internet Explorer ActiveX controls should be able to connect to the Internet and download, information from HlTP servers. No matter how much information you provide along with your control, there will always be more, up-to-date information on a server. An interesting, and in many cases necessary, feature you may want to add to your controls is the ability to connect to H1TP servers and download information on request. The ActiveX controls you design with VIsual Basic support asynchronous downloading of property values. This means that a property’s value may be a file on
your company’s Web server, and the custom control can contact the server and download information at will. To download a file from a URL, use the AsyncRead method, whose syntax is:
The type of the file to be downloaded is specified with the AsyncType argument, which can be one of the constants
The last, optional, argument is the name of the property to be downloaded. This name ts simply an identifier that you can use later to retrieve the value of the ,property downloaded or to cancel the downloading of the data. In the examples, I am going to use the PropertyName’argument to distinguish between multiple properties that are downloaded simultaneously. The PropertyName parameter can be any arbitrary name, since its only function is to act as an identifier for this particular data request.
Once the data is requested with the AsyncRead method, the program control returns to the application, which can continue with other tasks. ‘Thedownload may take a while, so the AsyncRead method performs an asynchronous operation. When the download is.complete, the AsyncReadComplete event will be raised. The code
for handling the downloaded data goes into the AsyncReadComplete event’s handler, whose definition is:
The AsyncReadComplete event will be raised even if an error occurred during the transmission. H the download didn’t complete successfully, a runtime error will occur when you access the V.a1ueproperty of the AsyncProperty object. You should always include an On Error statement in the AsyncReadComplete event handler to trap download errors.
The Value property is the actual value of the property you’re downloading. If / the of the requested information was vbAsyncTypeFile, the Value property is the name of a temporary file, which VIsual Basic will create for you in the Temp folder on your disk. You can use the standard file I/O functions and statements to process it. If the type of the requested information was vbAsyncTypePidure, the Value property is a Picture object that holds the bitmap. You can assign the Value property to file Pictureproperty of a control (or the UserControl object), or you can transfer the bitmap with the PaintPicture method. Finally, if the type of the requested information was vbAsyncTypeByteArcay, Value is the array where the bytes are stored. It is possible to cancel the asynchronous download by calling the CancetAsync. Read method. The syntax of the CancetAsynckead method is:UserContro 1 CancelAsyncRead PropertYname PropertyName is the name of the property being downloaded (the last argument of the AsyncRead method). If Property Name is not supplied (because no such property was specified in the AsyncRead method), the last Asynckead method invocation that was called without a Property. name argument will be cancelled.
VB6 at Work: Rates Custom Control
The asynchronous downloading of property Va111e5 () rel the internet is demond strated in the Rates project, which is shown in Figure 21.12. The Rates Control Demo has a visible interface that consists of a UstBox contro., where the most up-to-date curiency rates are displayed. The Rates Control Demo also provides methods for retrieving the rates for various currencies so that the control can remain hidden ..
The Rates Control’s Members
These members were added t’) :he control with the help of the Wtzardand they are all mapped to the equivalent members of the ListBox control: In addition tothe standard members, the Rates control provides the following custom members.
DownloadRates Method The DownIoadRa~ method connects to the server and downloads the document with the currency rates. Its syntax is:
Download Rates (RatesURL As String)
RatesURL is the t)RL of a text file with the following structure:
The first line contains the date of the file’s last update so that the client knows how recently the rates have been updated. The following lines contain currency symf)ols and rates. separated with commas. Here are the lines of the text file that populated the list in Figure21.12
Since the code uses the comma to separate the two items on each line, you can’t use the same character to format numbers, The value 2340.500 can’t be listed in the Rates.txt file as 2,340.500, This will throw off the code that parses the file and populates the ListBox control.
RatasRead Event This event is triggered as soon as the data is downloaded.
LastUpdate Property This property is a date valuethat returns the date and time the rates were last updated. The value of this property is read nom the server, along with the currency rates.
Count Property This property returns the number of currencies downloaded from the server.
GetCurrencyValue(currency As String) This method returns the exchange rate for the specified currency. To find out the exchange rate for the German mark, for example, call the GetCurrencyValue as follows:
DMRate – GetCurrencyValueC”QM”)
GetCurrencyName Method This method returns the name of the currency .in the location index of the UstBox control.
DloadErrorC.:rrNumber As Long. ErrDescription As String) The DLoad- Error event is triggered from within the AsyncReadComplete event, to notify the application that an error has occurred. The host application can read the error’s number and description and act accordingly.