![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
These other methods do not return the downloaded data in the same way that OpenURL does. Rather, the data is placed in the Internet Transfer controls internal buffer. Before getting to the details of the methods, we need to see how you retrieve data from this buffer. First, lets look at the Internet Transfer controls single event, which is essential for retrieving data from the buffer. What State Am I In? The Internet Transfer controls one event occurs whenever the state of the control changes, reflecting a change in the connection status of the control, the receipt of some data, or some other communication event that your program may need to know about (including, as we will see, errors). The associated event procedure has the following declaration: Inet1_StateChanged(ByVal State As Integer) State is a value identifying the new state of the control. The possible values, the defined constants, and their meanings are described in Table 18.1. Using the StateChanged event is an essential part of using the Internet Transfer controls advanced methodsthat is, methods other than OpenURL. Well see how soon. You can also use this event to provide status messages to the user, giving information on what tasks the control is doing or has completed. Listing 18.1 shows the code to do this. In this example, I am using a Text Box control named txtStatus to display the status messages. You could just as well use various other methods, such as a status bar, to display the messages. You should note that the control state identified by the number 11, icError, indicates that an error has occurred. But what error? Lots can go wrong when trying to transfer information over the Internet. Fortunately, the Internet Transfer control has two properties, ResponseCode and ResponseInfo, that provide a numerical code and a text description
of the most recent error. You can see that the code in Listing 18.1 uses these properties to provide the user with a description of any error that occurs. Listing 18.1 Using the StateChanged event to provide status messages to the user. Private Sub Inet1_StateChanged(ByVal State As Integer) Select Case State Case icResolvingHost 1 txtStatus.Text = Looking up IP address of host computer Case icHostResolved 2 txtStatus.Text = IP address found Case icConnecting 3 txtStatus.Text = Connecting to host computer Case icConnected 4 txtStatus.Text = Connected Case icRequesting 5 txtStatus.Text = Sending a request to host computer Case icRequestSent 6 txtStatus.Text = Request sent Case icReceivingResponse 7 txtStatus.Text = Receiving a response from host computer Case icResponseReceived 8 txtStatus.Text = Response received Case icDisconnecting 9 txtStatus.Text = Disconnecting from host computer Case icDisconnected 10 txtStatus.Text = Disconnected Case icError 11 txtStatus.Text = Error & Inet1.ResponseCode & _ & Inet1.ResponseInfo Case icResponseCompleted 12 txtStatus.Text = Request completed - all data received End Select End Sub You should be aware that StateChanged events are generated by the OpenURL method, even though, given the fact that this method operates synchronously, the events are not really needed. Problems can arise in a program that uses OpenURL, as well as the other asynchronous methods, because the StateChanged event will be fired, and the associated code executed, at times when it is not needed and may, in fact, cause mischief. The solution I use for this potential problem is to maintain a global flag that indicates when OpenURL is active: Dim UsingOpenURL As Boolean ... UsingOpenURL = True buf = Inet1.OpenURL(...) UsingOpenURL = False Then in the StateChanged event procedure: Private Sub Inet1_StateChanged(ByVal State As Integer) If UsingOpenURL Then Exit Sub ... End Sub
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |