Using the Rates Control VB Help

Before examining the actual code of the control, let’s see how it’s used in the test project. The test project consists of a single Form that contains an instance of the Rates control and a.Command button. When the button is clicked, the control contacts a Web server and requests the exchange rates of various currencies. The
code behind the Get Rates button calls theDownl.oadkates method, passing as argument the URL of a text file.

The Rates.txt file in this example is in the root folder of the Web server I have installed on my computer. If you have the Internet Information Server or the Personal Web Server installed on your computer or on another computer on a local area network, place the Rates.txt me there, and connect to the server machine to download it. The address is, which is the address of the local machine. If you don’t have a Web server, see the instructions in the section “Testing the Rates  Control” for information about how to post the file on your Internet service provider’s server.

The Rates Control’s Code

The standard properties of the control were added with the Acti veX lnterface Wizard. We are going to focus on the procedures that download the text file and populate the List- Box control

The method calls the AsyncRead function, passing the URL of the Web server as an argument. The downloaded information will be stored in a file, and the property name is Rates.
The interesting action takes place in the AsyncReadComplete event of the Userf.ontrol object.

The code starts by examining the value of the AsyncProp object’s PropertyName property. In the Rates control, this p~perty can only be Rates. If the control is downloading ‘. multiple files at once, this property tells you which file completed downloading so that you can process it fro-mwithin your code. When the AsyncRead methods downloads a file, the AsYncProp object’s Value property is the name of the file where the file was stored on the local disk. It has nothittg to do with the actual filename on the server. It’s a unique filename created by the ‘system in the Temporary Internet Files folder, The name of the file is used by the ReadRatesO subroutine to display the rates ill the ListBox control.

This is straightforward VB code that opens a text file and appends its lines to the CurrencyList ListBox control. In addition, it stores the currency rates to the AllRates collection, using the currency name as key. This technique will simplify the retrieval of a currency’s exchange rate. The ReadRatesO subroutine is private to the UserControl object, and it can’t be called from the host application. Notice the error trapping code in all subroutines. When downloading information from the Internet, any number of things can go wrong. The H1TP server may be down, files may be rearranged on the server, transmission errors can occur, and so on. These errors must be trapped and dealt with in a robust manner One feature you might want to add to this project is to keep track of the property being downloaded and prevent the host application from initiating another . download of the same property while it’s being downloaded. Our code will give a generic error message (“Error in downloading rates”). The AsyncRead method can be called to download a file while another one is being downloaded, but it can’t download the same file twice Simultaneously.

Testing the Rates Control

Testing a control that downloads property values from an H1TP server requires that you connect to the server and the document to be used for testing the control be posted to the server. If you have your own Web server (Internet Information Server or the Personal Web Server), you can just copy the Rates.txt file to one of the Web server’s virtual directories. You can just copy it to the root directory and then connect to your own server using the following URL

The address is the address of your own computer on the network. Actually, you don’t even need a network. As long as you have a Web server installed on your computer, you can use this URL to connect to it. If the Web server is on another . computer on the same network, substitute the actual computer name for the string “127.0JU”.

Most of you, however, don’thave you own Web server. You can still use your Internet service provider’s server, as long as you have permission to post files on it (most ISPs offer a few megabytes to their subscribers, where they can post user pages). To test the Rates control with your ISP’s server, you must post the Rafes.txt file to your directory on the server. To do so, follow,:these steps:

1. Create a new folder on your hard disk, and place ‘.he Rates. txt file in-‘h (if you have any HTML files you would like to pos .place them there as well).
2. Start the Web Publishing Wtzard (a utility that comes with Wmdows and FrontPage
3. The first page of the Web Publishing Wizard is a welcome screen, Click Next button to display the Select a File or Folder window:

4. II} the File or Folder Name box, enter the name of the folder you JUSt Cated. Click Next.
.5. In the next window, specify a friendly name for the Web server and click Advanced button. You will see another window, where you must select tJ protocol to be used for transferring the file. .
6. Select FTP and click Next.

The process I describe here will work with many ISPs, but the exact same steps may not apply to all ISPs. If you have problems posting your files to the server, find out from your ISP what it takes to publish your own user, or personal, Web site. Instead of an entire Web site, just place the Rates.txt there. Or, if you already ha-e a Web site, place the Rates.txt file in the same directory. It’s not going to affect y:.our.Web,since none of the pages contain hyperlinks to this file. I£you download the rates and a few minutes or a few hours later you click the Download Ratts button again, chances are that the s~me values will appear on the control. You may have posted new files on the server several times, but the ratesct won’t change. Experienced Web surfers have already guessed what’s happening.

The AsyncRead method doesn’t contact the server to download the Rates.txt file again. It retrieves it from the cache, stores the information in another temporary file, and the control’s code reads it as usual. In effect, the AsyncRead method goes through the InternetExplorer Class and uses the same cache as Internet Explorer. Where Internet Explorer 4 allows you to refresh a page, the AsyncRead method doesn’t provide an argument that will force the data to be read from the server instead of the cache. To bypass this problem, you must change the Cache settings of Internet explorer. Follow these steps

1. Start Internet Explorer and choose View >- Internet Options.
2 Click the Settings button, to open the Settings dialog box:
3. To force Internet Explorer to reload a page from the server (instead of reading from the cache), click the Every Visit to the Page option. This action will also affect Internet Exp\orer’s operation. Internet Explorer will not use its cache; and every time you move back to a”page, even within a few seconds, the page will be reloaded from the server. As this book was ready to go to the press, Microsoft announced the first public beta of Internet Explorer 5. It’s also p0ssible that a new AsyncRead method may become available that allows you to specify whether a single page is downloaded or simply refreshed from the cache. Another interesting approach is to include the IntemetExplorer Class to the custom control and call its Refresh2 method to force the download of the file. When the AsyncRead method is called again, it will find the data in the cache, but the cache will contain the most recent data

Downloading Image Properties

The AsyncRead method can also download picture properties. Pictures can be downloaded as binary files and displayed with the LoadPicture method. VISual Basic can also create a device context and store the downloaded bitmap there. To download an image, call the AsyncRead function with the following arguments:

The constant vbAsyncTypePicture tells Visual Basic to download a picture object. No file is created on the host computer, and you can’t read the bitmap’s pixels. To use the image, you must monitor the progress of the download from within the AsyncReadComplete function. When the download of the property Image (or whatever name you have assigned to the property in the AsyncRead method)
completes, you must retrieve the property’s value and store it in a Picture object with a statement such as the following:

If you’re downloading multiple images, use different property names to differentiate them. You can then use the Bitmap object’s properties, such as Width and Height. The following code segment creates a Picture object with the downloaded bitmap and then calls the Showlmaget) subroutine to display the image on a PictureBox control.

In S1.IIlUIW’)’, downloading image pro~ from an HITP server is quite analogous to downloading text files. You must specify the type of data to be downloaded in the AsyncRead function (obAsyncTypePicture constant instead of vbAsync’IypeFile). When the image is downloaded, a Picture object will be automatically aeatedr where the image’s bitmap will be stored Gust like the text file is generated automatically for you). You can then use the PaintPicture method to transfer the bitmap from the Picture object to a PictureBox control or the User- Control object itself.

Finally, you can download data in array format, by specifying the constaftt abAsynclyPeAtray in the AsyncRead function. This time VISual Basic will create a byte array, and ypu must process it from within your code.

Posted on November 6, 2015 in Visual Basic and the Web

Share the Story

Back to Top
Share This