Internet Applications using VB.NET

Using Internet Explorer in your Application

Most of the modern applications will display HTML files and other files commonly used with the Internet applications. The .NET Framework provides considerable support for common image formats such as JPEG, GIF and PNG. The need to write our own HTML parser to display HTML pages is eliminated. You can use the existing component to display HTML pages. Microsoft’s Internet Explorer was implemented as a stand-alone Component Object Model (COM) object. We can use COM objects in our VB.NET applications. Let’s see how?

Using Visual Basic .NET create a new Windows Control Library. From the code window delete the default UserControl1 and create a new control called web1.

From the Design window, right-click on ToolBox and select Add/Remove Items. Next, select the COM Components tab, scroll down until you find Microsoft Web Browser, click on the check box and press the OK button. 

Add a new Panel to the design form surface. Name the Panel to Panel1. Now we need an address bar at the top of the control. Add a TextBox control. Clear the text Property to blank and name the TextBox property as texturl. Set the anchor to top left.

Drag and drop a new Microsoft Web Browser control on to the designer surface. Name the control as WebBrowser. When you drop this control on the designer surface, new assemblies will be added to the list of references for the project. These assemblies contain a class called Web Browser.

To test our control we need to get it to load a page. We will add a WebUrl property and make it navigate to that location when the control is loaded. Add the following members to web1.

Public Class web1
Inherits System.Windows.Forms.UserControl

Private _url As String
Private web_url As String = "http://www.developeriq.com/"

End Class


When the WebUrl property is set, we will just update the web_url field. When the URL property is set, we will ask the WebBrowser control to navigate to the URL. Refer code 1.

Public Property WebUrl() As String
Get
Return web_url
End Get
Set(ByVal Value As String)
web_url = Value
End Set
End Property

Public Property url() As String
Get
Return _url
End Get
Set(ByVal Value As String)
WebBrowser.Navigate(Value)
End Set
End Property

You will notice that we can call the navigate method on WebBrowser. To navigate to the URL of our choice, we need to set the AcceptsReturn property of the texturl control to true. Add a handler of the KeyPress event of the texturl to check whether the user has hit the return key. Add the following code 2 in the code window.

Private Sub texturl_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles texturl.KeyPress
if e.KeyChar = char(13) then
url = texturl.Text
End If

End Sub


Save the application and build solution for creating web1.dll. Create a new Windows Application Project and add control to Toolbox, right-click on the windows forms tab in the Toolbox and select ‘Add/Remove Items…’ and click on the .NET Framework Components tab. Click on the browse button and navigate to the directory where your web1.dll exists. Once you select the DLL, select the check box and click on the OK button. Drag and drop the web1 control to the form surface and set its Dock Property to Fill.

Run the project and enter the URL in the textbox and press enter key. 

On the browser when you click on the links, the text in the textURl is not updated. For that we need to write the code. To change the url in the texturl add code 3 in the web1 browser control.

Private Sub WebBrowser_DownloadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles WebBrowser.DownloadComplete
_url = WebBrowser.LocationURL
texturl.Text = url
End Sub


To get the URL we need to use the LocationURL property of the WebBrowser.

Adding Toolbar

Now we will add a simple toolbar at the top of the control that gives us the usual features from a Web Browser, namely back, forward, stop, refresh and home.

Add the five buttons to the forms surface of the WebBrowser usercontrol and name the buttons as button_back, button_forward, button_stop, button_home, and button_refresh. To get the resizing to work properly, make sure you set the Anchor property on the right to top, right. On startup of the browser you should disable button_back, button_forward and button_stop. Add the following functionality behind the buttons (code 4).

Private Sub button_back_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button_back.Click
goback()
End Sub
Public Sub goback()
WebBrowser.GoBack()
End Sub


Private Sub button_forward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button_forward.Click
goforward()
End Sub
Public Sub goforward()
WebBrowser.GoForward()
End Sub


Private Sub button_refresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button_refresh.Click
WebBrowser.Refresh2()

End Sub

Private Sub button_home_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button_home.Click
WebBrowser.GoHome()

End Sub

Private Sub button_Stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button_Stop.Click
WebBrowser.Stop()
End Sub

To manage the enable and disable feature of the buttons, we have to write steps in a couple of the events. Whenever downloading begins, we need to enable stop and disable refresh buttons. Add the DownloadBegin event handler of WebBrowser control to enable and disable the buttons (code 5).

Private Sub WebBrowser_DownloadBegin(ByVal sender As Object, ByVal e As System.EventArgs) Handles WebBrowser.DownloadBegin
button_Stop.Enabled = True
button_refresh.Enabled = False

End Sub


Add code 6 in the DownloadComplete event handler to switch the buttons around.

Private Sub WebBrowser_DownloadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles WebBrowser.DownloadComplete
_url = WebBrowser.LocationURL
texturl.Text = url
button_Stop.Enabled = False
button_refresh.Enabled = True
End Sub


The CommandStateChange event lets WebBrowser tell us when we update the enabled or disabled states of our buttons. This event provides an ID, so we know which button has been enabled or disabled, and a Boolean flag indicates whether or not we should enable it or disables it (code 7).

Private Sub WebBrowser_CommandStateChange(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_CommandStateChangeEvent) Handles WebBrowser.CommandStateChange
Dim control As Control = Nothing
Select Case e.command
Case 1
control = button_forward
Case 2
control = button_back
End Select


If Not control Is Nothing Then
control.Enabled = e.enable
End If
End Sub


Save and rebuild the control. Add this control in your Windows application project and run the application. Type the URL and you can notice the back, forward and stop buttons are disabled. 

Conclusion

In this article we have seen how to reuse COM-based Internet Explorer control in our own Windows Form application.



Added on July 24, 2007 Comment

Comments

Post a comment