• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3061
  • Last Modified:

Embedding the IE to a VB.NET form.


Hello:

I'm trying to create an application that will allow me to embed IE into a VB.NET (2003) form.

My idea is to have something like this:

' being vb.net form
------------------------------------------------------
(back) - (forward) - (stop) - (refresh)
------------------------------------------------------
  DISPLAY HTML PAGES
 
 
------------------------------------------------------
' end vb.net form

In other words the form will have like a top part with the buttons or controls like back, forward,
stop, etc. and the bottom part is where the actual HTML will be display.  The URL’s will be hardcoded
in the code.

I assuming the way to do this is by embedding the IE Object into the form, so far I’m using "shdocvw"
my code looks as follows, but it opens a NEW IE window. I need it to be embedded into the form and have
the ability to control it and go back, forward, stop, etc.


' VB.NET 2003 CODE
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
[....]
#End Region
    Public WithEvents IE As SHDocVw.InternetExplorer

    Public Sub Form1_Load()

    End Sub

    Private Sub Form1_Unload()
        IE.Quit()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        IE = New SHDocVw.InternetExplorer
        IE.Visible = True
        IE.MenuBar = False
        IE.AddressBar = False
        IE.ToolBar = False
        IE.Navigate("http://www.yahoo.com")
    End Sub
End Class
0
mhertzDEV
Asked:
mhertzDEV
  • 3
1 Solution
 
S-TwilleyCommented:
On your toolbox.. goto customize

Check the "Microsoft Web Browser" control, this will add a control that you can add to your form instead of an IE instance
0
 
S-TwilleyCommented:
You can control it much like the InternetExplorer object, except its embedded in your application. If you have any probs with it, just ask
0
 
LacutahCommented:
0
 
mhertzDEVAuthor Commented:
Thanks a lot S-Twilley...

Here is my current code, to help the next guy =)

Any suggestions will still be appreciated.


' BEING VB.NET CODE
Imports

Public Class Form1
    Inherits System.Windows.Forms.Form


#Region " Windows Form Designer generated code "

[.......]

#End Region


    Public Shared Function Main(ByVal args() As String) As Integer

    End Function


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AxWebBrowser1.Visible = True
        AxWebBrowser1.AddressBar = True
        AxWebBrowser1.Navigate("http://www.yahoo.com")
    End Sub

    Private Sub btnGoBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGoBack.Click
        'If there is nothing to go back too and we click GoBack,
        'it will try to access an object that doesnt exists and
        'BIGBADABOOM! Unless we next it =)
        On Error Resume Next

        AxWebBrowser1.GoBack()
    End Sub


    Private Sub btnGoForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGoForward.Click
        'If there is nothing to go forward too and we click GoForward,
        'it will try to access an object that doesnt exists and
        'BIGBADABOOM! Unless we next it =)
        On Error Resume Next

        AxWebBrowser1.GoForward()
    End Sub

    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        AxWebBrowser1.Stop()
    End Sub


    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        AxWebBrowser1.Refresh2()
    End Sub

    Private Sub AxWebBrowser1_StatusTextChange(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEvent) Handles AxWebBrowser1.StatusTextChange
        StatusBar1.Text = AxWebBrowser1.StatusText
    End Sub

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
      'AxWebBrowser Printing current document with Print Dialog
        AxWebBrowser1.Document.parentWindow.execScript("window.print();", "JScript")
    End Sub
End Class
0
 
S-TwilleyCommented:
I'd use the Try catch block instead of Resume next, might not make much of a difference here, but it's a more powerful error handling method:


Try
    AxWebBrowser1.GoBack()
Catch e as exception
    'Im guessing you're just ignoring errors so don't do anything here
End Try

===================

Also add a textbox so you can choose where to navigate to (change webpage on enter being pressed... or a Go button)
To match this, on the NavigateComplete event of the browser control. Add the line

txtAddress.Text = AxWebBrowser1.LocationURL

Note: the navigatecomplete event is fired for subframes as well, but the property returns from AxWebBrowser1 is the hosting webpage

===================

You may also want to rename the control to something shorter, cos in my experience, you'll be typing that out alot

===================

I'd put the web control in it's own panel, and set the dock property of it to fill. Then anchor the panel to all sides, so that when you resize the window, it follows it around
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now