Beginning Visual Basic 2005 Express Edition - From Novice To Professional (2006)
.pdf450 |
C H A P T E R 1 7 ■ T H E I N T E R N E T A N D V I S U A L B A S I C |
Figure 17-1. Drop a status control onto the form.
You’ll use this control to display a progress bar as the web page loads. To do that, click on the drop-down arrow that’s displayed on the status bar and choose ProgressBar from the list that appears. This will drop a progress bar onto the status bar, as you can see in Figure 17-2.
Figure 17-2. Add a progress bar to the status bar by using the drop-down list inside the status bar.
Don’t worry about setting up the names of the status bar or its progress bar—we’ll keep the app simple.
Next, if you take a look at the bottom of the list of Common Controls in the Toolbox, you’ll see the WebBrowser control in Figure 17-3.
C H A P T E R 1 7 ■ T H E I N T E R N E T A N D V I S U A L B A S I C |
451 |
Figure 17-3. The WebBrowser control lives at the bottom of the Common Controls collection in the Toolbox.
Double-click the control to drop it onto the form. You won’t actually see anything because by default the WebBrowser control docks within the window and takes up all available space. You will notice that the control has a smart tag like most of the new .NET controls, and the inside of the form will turn white (see Figure 17-4).
Figure 17-4. Dropping the WebBrowser control onto the form doesn’t really show anything special in design mode.
Click on the WebBrowser inside the form and take a look at the properties. The properties that are of most interest to us live in the Behavior group of properties in the window, and control how the browser works—for example, whether the browser will have scroll bars, or whether it will accept the Microsoft Internet Explorer keyboard shortcuts, and so on. For now, just set the Url property to http://www.microsoft.com. This
452 |
C H A P T E R 1 7 ■ T H E I N T E R N E T A N D V I S U A L B A S I C |
sets up the default website that will display in the browser when the application runs. You can see this in Figure 17-5, along with the other Behavior properties.
Figure 17-5. The Behavior properties of the WebBrowser control
Let’s take a look at the events now. Click the Events button of the Properties window to switch to event view (see Figure 17-6).
Figure 17-6. The events supported by the WebBrowser control
C H A P T E R 1 7 ■ T H E I N T E R N E T A N D V I S U A L B A S I C |
453 |
The event model of the WebBrowser control pretty much lets you hook into everything the control is doing, from navigating as a result of a click, to all stages of downloading and displaying a page. You can see them in the Action and Behavior groups of events.
Double-click the ProgressChanged property to drop into the code editor for that event. The ProgressChanged event, as its name suggests, fires to let you know the progress of an operation has changed. It’s specifically geared to people who want to write code to update a progress bar. The
WebBrowserProgressChangedEventArgs parameter e, for example, has two properties (Maximum and
Value) that are specifically geared toward that task. So, your code gets real simple:
Private Sub |
WebBrowser1_ProgressChanged(ByVal sender As System.Object, _ |
ByVal e |
As System.Windows.Forms.WebBrowserProgressChangedEventArgs) _ |
Handles |
WebBrowser1.ProgressChanged |
ToolStripProgressBar1.Maximum = e.MaximumProgress
ToolStripProgressBar1.Value = e.CurrentProgress
End Sub
Here you’re just copying those two values out of the event parameter e and dumping them straight into your status bar’s progress bar, and voilà—you have an updating status bar.
Well, almost.
The event will fire every time the browser does something in the progress of rendering a page, but it won’t always fire when the page load is complete. So you should hook up the browser’s DocumentCompleted event. Go back to the form editor, select the WebBrowser control, and then find the DocumentCompleted event in the event browser and double-click it to once again drop into the code window. Add the following highlighted lines to the DocumentCompleted event handler:
Private Sub |
WebBrowser1_DocumentCompleted(ByVal sender As System.Object, _ |
ByVal e |
As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _ |
Handles |
WebBrowser1.DocumentCompleted |
ToolStripProgressBar1.Value = ToolStripProgressBar1.Maximum
End Sub
That’s all there is to it—your simple browser app is complete. Run the program now and you’ll not only see Microsoft’s home page load, but the progress bar will update as well (see Figure 17-7).
454 |
C H A P T E R 1 7 ■ T H E I N T E R N E T A N D V I S U A L B A S I C |
Figure 17-7. The WebBrowser control, once set up, takes care of almost everything.
Feel free to click on links, and also to right-click on the browser to bring up the familiar Internet Explorer context menu that lets you navigate back and forward through pages, among other things.
Working with the WebBrowser Control
In the preceding example, you looked at the most obvious property of the control, the Url property, and also two of its events. There’s a lot more that you can do to interact with the control through code, and surprisingly enough, for such a powerful control it’s all very easy.
Take a look at the main methods that the control supports in Table 17-1, for example.
C H A P T E R 1 7 ■ T H E I N T E R N E T A N D V I S U A L B A S I C |
455 |
Table 17-1. Methods of the WebBrowser Control
Method Description
GoBack() Go back one page. Obviously, this works only if you’ve clicked on a link already.
GoForward() Go forward one page. Works only if you’ve gone “back” from a page.
GoHome() Go to IE’s default home page.
GoSearch() Go to IE’s default search page.
Navigate() Take a string URL to navigate to and go there.
The methods are all pretty simple to understand, so we’re not going to dive into a fullblown “Try It Out.” Feel free to experiment with them yourself.
Perhaps the most useful aspect of the control is that it’s not limited to just web browsing. You can use the WebBrowser control to easily download files from the Internet, and even to build a fully functional FTP client right into your application. All you need to do is call Navigate with an FTP:// url. For example, if you call Navigate() with an address of ftp://ftp.gnu.org, you’ll find yourself looking at the GNU FTP site, shown in Figure 17-8.
Figure 17-8. Passing an FTP address to the WebBrowser control turns it into a neat FTP client.
456 |
C H A P T E R 1 7 ■ T H E I N T E R N E T A N D V I S U A L B A S I C |
Double-clicking a directory here will take you into a separate FTP browser window from which you can drag and drop to download files, just as if you were working with an FTP site in the standard Windows Explorer way (see Figure 17-9).
Figure 17-9. Double-clicking an item opens up a standard Explorer window to browse and download the selected resource.
Accessing the Web Through Code
Of course, you won’t always want to display a WebBrowser control every time you want to do something on the Internet. Often it’s quite handy just to be able to go out on the Internet and do something without the user seeing. For example, I wrote a program a while back that would automatically check for new videos on Microsoft’s Channel 9 site and download them. Rather than display a WebBrowser control every time it did this, the code just downloaded the videos page and examined it to see whether there were new videos that I should get. If there were, the code would download them and just display a progress bar to me.
C H A P T E R 1 7 ■ T H E I N T E R N E T A N D V I S U A L B A S I C |
457 |
Listing that application as a “Try It Out” would take more pages than I’m allowed, but I can talk you through the general principle here. The root of the solution is a class in the
.NET Framework called System.Net.WebClient. This gives you all the functionality of a browser, without the visual aspects, making it ideal for doing web-type stuff behind the scenes of the application’s user interface. Let’s take a look.
Try It Out: Using System.Net.WebClient
Let’s write a really cool app. We’re going to have the application extract data from Apress’s page of forthcoming books. The titles of the books will then be listed in alphabetical order in a list box, and double-clicking an item will cause your default web browser to run and to display information on just that book in Apress’s catalog. Hopefully by now you’ve begun to spot the trend whereby scary-sounding projects turn out surprisingly easy to do with .NET. This one is no exception.
Start up a brand new Windows Forms application; I named mine NewBooks. When the form editor appears, drop a Button control and a ListBox control onto the form. Also put a WebBrowser control on the form but set its Visible property to False. In design mode, your form should end up looking like the one in Figure 17-10.
Figure 17-10. Arrange controls on your page like this. The control at the top left of the form is the WebBrowser control; set its Visible property to False so that it doesn’t show at runtime.
458 |
C H A P T E R 1 7 ■ T H E I N T E R N E T A N D V I S U A L B A S I C |
I know I said that we wouldn’t be using a WebBrowser control in this example, and I’ve just made you add one to your form. Don’t panic. The WebBrowser control can be used to open a real browser, and that’s all we’re going to use it for in this example.
Set up the names and properties of the controls on your page as shown in Table 17-2.
Table 17-2. Properties of the Controls for Your Form
Control |
Property |
Value |
ListBox |
Name |
bookList |
|
Sorted |
True |
Button |
Name |
bookCheckButton |
|
Text |
Check for books |
WebBrowser |
Name |
browser |
|
|
|
When you’ve finished laying out your form, double-click the Check for Books button to drop into the code editor for its Click event. Thinking about our goals, you’ll need to grab the web page, extract the data, and then fill in the list box contents. So, the first thing to do is write the code to grab the web page itself.
You’ll store the web page source in a string variable, and you’ll write a separate function to use System.Net.WebClient to grab the web page source. So, add the following line of code to the click event handler:
Private Sub bookCheckButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles bookCheckButton.Click
Dim newBooksPage As String = _
GetWebPage("http://www.apress.com/book/forthcoming.html")
End Sub
C H A P T E R 1 7 ■ T H E I N T E R N E T A N D V I S U A L B A S I C |
459 |
So, you call a function called GetWebPage and pass in the URL of the page you want. Let’s write that function now. Directly underneath the click event handler, add this method, and after you’re finished I’ll talk you through how it works (you’ll also need to add an Imports statement to the top of your class, as shown):
Imports System.Net
Public Class Form1
Private Sub bookCheckButton_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles bookCheckButton.Click
Dim newBooksPage As String = _
GetWebPage("http://www.apress.com/book/forthcoming.html")
End Sub
Private Function GetWebPage(ByVal url As String) As String
Dim web As New WebClient()
Return web.DownloadString(url)
End Function
End Class
DownloadString() is one of a number of methods on the WebClient class that exist to download something from a remote server. In this case you’re just telling WebClient that the resource you want to download (a web page) can be stored in a single string. All the code does is return whatever DownloadString() gives you.
Okay, at this point when the user clicks the button, you’ve managed to use WebClient to hit Apress’s website and grab the page with all their forthcoming books on it. The next problem to tackle, of course, is extracting the book names and URLs for more information on them. First, you’re going to need a place to store that information.