web browsing objects
Post on 21-Apr-2015
Embed Size (px)
Web Browsing Objects
Integrating Visual Basic with the Internet
Web Browsing Objects The two objects weneed in order to add Web techniques and hyperlinked documents to our Visual Basic applications are: 1. The WebBrowser control 2. The InternetExplorer object The WebBrowser is an ActiveX control that can display HTML documents on Visual Basic Forms. InternetExplorer is an OLE Automation object that you can use to control Microsoft Internet Explorer (and the WebBrowser control) from within your code. The two objects have many common members
The WebBrowser Control Simply put, the WebBrowser control is Internet Explorers window. Any HTML document that can be displayed in Internet Explorer can also be displayed in the WebBrowser control. In other words, the WebBrowser control adds browsing capabilities to your Visual Basic applications. It allows the user to browse sites on the World Wide Web, local files, or ActiveX documents, such as Word or Excel documentsall from within a Visual Basic application. Because the WebBrowser is an ActiveX control, you can place it on any Visual Basic Form.
Steps to add WebBrowser ControlBefore you can use it, however, you must add it to the Toolbox 1.Right-click the Toolbox, and from the shortcut menu, select Components to open the Components dialog box. 2. Select Microsoft Internet Controls, and then click OK. Two new icons will appear on the Toolboxthe WebBrowser controls icon and the ShelFolderViewOC controls icon. 3. Select the WebBrowser controls icon and draw an instance of the control on the Form.
The WebBrowser Control When you place a WebBrowser control on a Form, its a borderless rectangle that you can size in any way you like. Because the control cant be resized by the user at runtime, you should try to adjust its size according to the size of its container, which is a Visual Basic Form. When the user resizes the Form, the WebBrowser control should be resized also so that it covers most of the Form. To display a Web page in the WebBrowser control, use the Navigate method. You can also move through the list of URLs that have been displayed already with the GoBack and GoForward methods. The WebBrowser control automatically maintains the list of visited URLs.
The WebBrowser Control
The InternetExplorer Object The InternetExplorer object allows you to start an instance of Internet Explorer from within your application and manipulate it through OLE Automation. The InternetExplorer object supports the same properties and methods as the WebBrowser control, plus a few more.
Steps to add InternetExplorer Control1. Start a new project and select Standard EXE as the project type. 2. Choose Project References to open the References dialog box: 3. Check the Microsoft Internet Controls checkbox. This time, you wont see a new icon in the Toolbox. But if you open the Object Browser window, you will see that the InternetExplorer Class has been added to the project. In the Members window, you will see the properties and methods exposed by the InternetExplorer Class, and through these members you can OLE automate the Internet Explorer application.
The Properties of the WebBrowser Control and the InternetExplorer Object 1. Application This property returns the automation object whereHTML documents are displayed (the WebBrowser control or the InternetExplorer object).
Busy This property returns a True/False value specifying whetherthe control is navigating to a new URL or is downloading a Web page. If the controls Busy property is True for an unusually long time, call the Stop method to cancel the navigation or the download of a document.
Container This property returns an object that evaluates to thecontainer of the WebBrowser control.
The Properties of the WebBrowser Control and the InternetExplorer Object 4. Document This property returns the automation object of the activedocument, if any. This is not the Document object of the Scripting Model. To access the document displayed on the control, you use the following expression:
Height, Width These two properties return the dimensions, inpixels, of the control that contains the WebBrowser control.
Top, Left These two properties return the location, in pixels, ofthe controls upper-left corner on the Desktop.
The Properties of the WebBrowser Control and the InternetExplorer Object7. LocationName This property returns the title of the Web pagedisplayed on the WebBrowser control. 8. LocationURL This control returns the URL of the page displayed on the WebBrowser control. The LocationName and LocationURL properties retrieve information about the location of the displayed document. If the location is an HTML page on the World Wide Web, LocationName retrieves the pages title, and LocationURL retrieves the URL of that page. If the document displayed is a local file, both LocationName and LocationURL retrieve the full path of the file (or its UNC, if its located on a network). 9. Type This property returns a string that determines the type of the contained document object. The type for HTML documents is Windows HTML Viewer.
The Methods of the WebBrowser Control and the InternetExplorer Object1. GoBack, GoForward These two methods navigate backward or forward one item in the history list, which is maintained automatically by the WebBrowser control or the InternetExplorer object. Attempting to move after the most recent URL or before the first URL in the list generates a runtime error. To prevent this, you must include some error-trapping code, similar to the code you saw in the IExplore application, earlier in this chapter. 2. GoHome, GoSearch The GoHome method navigates to the current home page; the GoSearch method navigates to the search page, as specified in the Internet Explorer Options dialog box.
The Methods of the WebBrowser Control and the InternetExplorer Object3. Navigate This method navigates to a URL or opens an HTML file,as specified in the methods first argument.This method has the following syntax:Navigate URL [Flags,] [TargetFrameName,] [PostData,] [Headers]
Refresh This method reloads the page currently displayed on theWebBrowser control.
Refresh2 This method is similar to Refresh, but it lets you specifythe refresh level. It has the following syntax:
The Methods of the WebBrowser Control and the InternetExplorer Object
6. Stop This method cancels any pending navigation ordownload operation and stops playback of multimedia elements such as background sounds and animations.
The Events of the WebBrowser Control and the InternetExplorer Object1. BeforeNavigate2 This event occurs when the WebBrowser control is about to navigate to a different URL. It can be caused by external automation (by calling its Navigate method) or by internal automation from within a script or when the user clicks a hyperlink in the current document. Your application has an opportunity to cancel the navigation by setting the methods Cancel argument to True. 2. NavigateComplete This event occurs after the control has successfully navigated to the new location. Some of the documents resources may still be downloading (a large image, for instance, may take quite a while), but at least part of the document has been received from the server, and progressive rendering has started already. To interrupt this process, you must call the Stop method.
The Events of the WebBrowser Control and the InternetExplorer Object3. DownloadBegin This event occurs when a navigation operation is beginning. Its triggered shortly after the BeforeNavigate event (unless the navigation was canceled), and it signals your application to display a busy message or change the pointers shape. The DownloadBegin event has the following declaration: Sub WebBrowser1_DownloadBegin () 4.DownloadComplete This event occurs when a navigation operation is finished, halted, or failed. Unlike NavigateComplete, which may not be triggered if the navigation doesnt complete successfully, this event is always triggered after a navigation starts. Any busy indication by your application must end from within this event. The DownloadComplete event has the following declaration: Sub WebBrowser1_DownloadComplete ()
The Events of the WebBrowser Control and the InternetExplorer Object5.ProgressChange The WebBrowser control tracks the progress of a download operation and periodically issues the ProgressChange event to inform your application of the progress. The ProgressChange event has the following declaration: Sub WebBrowser1_ProgressChange(ByVal Progress As Long, _ ByVal ProgressMax As Long) 6.TitleChange This event occurs when the title of the current document changes. The title of an HTML document can change; while the document is being downloaded, the URL of the document is also its title. After the real title (if one was specified with the TITLE tag) is parsed, the TitleChange event is triggered, and you can use it to update the Caption property on your Visual Basic Form.
The Events of the WebBrowser Control and the InternetExplorer ObjectThe TitleChange event has the following declaration: Sub WebBrowser1_TitleChange(ByVal Text As String) 7. NewWindow Although most hyperlinks result in updating the same window in which the document with the hyperlink is displayed, some hyperlinks specify that a new window be opened to display the destination document. When a new window is about to be created for displaying a new document, the NewWindow event is triggered. 8. FrameBeforeNavigate, FrameNavigateComplete, FrameNewWindow These three events are identical to the BeforeNavigate, NavigateComplete, and NewWindow events, except that they are triggered from within frames.
Using Document Object
The Document Obje