Click here to Skip to main content
15,891,993 members
Articles / Desktop Programming / Win32
Tip/Trick

Tabbed IE Browser Control

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
3 Jul 2013CPOL2 min read 24.4K   8   9
Tabbed Browser Control addon

Introduction 

This DLL can be added to your tool box, and then dragged onto your form in place of the normal WebBrowser control. This one is tab based, has a custom context menu that works. 

Background 

I have read hundreds of posts on various programming sites like this one with people trying to get help with:

  • Opening link in new Tab
  • Stopping a link from opening in Internet Explorer instead of the program.
  • Using a custom Context Menu in place of the built in one. 

Having educated myself with the answers posted, I have developed this UserControl DLL that simplifies all of that. 

Using the code 

Simply add the .dll to your project by right clicking in your toolbox and selecting  "Choose Item..." and navigate to where you downloaded the Tabbed_Browser.dll. Then drag the new control from the Toolbox onto your form.

There are several Public Variables, Subs, and controls that you can manipulate with your own code. For example, you can add a button to your project, name it HomeBtn and labeled Home.  Now, in the click event (double click the button to automatically add to code) you can do:

VB
Private Sub HomeBtn_Click(sender As System.Object, e As System.EventArgs) Handles HomeBtn.Click
    Tabbed_IE 1.GoHome()
    'This activates the same public sub used by the Home contextmenustrip item. 
End Sub

or add a textbox and a timer to your project and use the following code to see what url the link or image under the mouse  points to. 

VB
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    TextBox1.Text = Tabbed_IE1.Linx 'Accesses the public string "Linx". 
End Sub

If you prefer to use your own address bar you can make mine go away by adding this line to your form load event: "Tabbed_IE.Addressbar.dispose()". 

If you want your tabs on the left instead of the top, you can do that in your form load event with this:

VB
Tabbed_IE1.Tabby.Alignment = TabAlignment.Left

There are many more things you can manage with your own code. 

Points of Interest 

I am working on doing the same thing with the Gecko control used by Firefox and Google Chrome.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralMy vote of 5 Pin
rspercy655-Jul-13 10:39
rspercy655-Jul-13 10:39 
QuestionWhere's the download link? Pin
gcode14-Jul-13 1:30
gcode14-Jul-13 1:30 
AnswerRe: Where's the download link? Pin
Emmery Chrisco4-Jul-13 3:24
Emmery Chrisco4-Jul-13 3:24 
GeneralRe: Where's the download link? Pin
Ravi Bhavnani12-Jul-13 5:46
professionalRavi Bhavnani12-Jul-13 5:46 
QuestionRe: Where's the download link? Pin
rspercy654-Jul-13 4:34
rspercy654-Jul-13 4:34 
AnswerRe: Where's the download link? Pin
Emmery Chrisco4-Jul-13 4:41
Emmery Chrisco4-Jul-13 4:41 
GeneralRe: Where's the download link? Pin
rspercy654-Jul-13 13:45
rspercy654-Jul-13 13:45 
GeneralRe: Where's the download link? Pin
Emmery Chrisco5-Jul-13 3:34
Emmery Chrisco5-Jul-13 3:34 
Imports System.Windows.Forms, System.Drawing

Public Class Tabbed_IE

#Region "Public Variables"
    Public MyURL As String
    Public MyLink As String
    Public MySource As String
    Public CurrentBrowser As WebBrowser
    Public DocTitle As String
    Public ImgName As String
    Public WithEvents NewURL As New myVar
    Public WithEvents CurrentDocument As HtmlDocument
    Public MousePoint As New Point
    Public Ele As HtmlElement
    Public Ment As HtmlElement
    Public Linx As String
    Public Sauce As String
    Public HomePage As String
#End Region

    Private Sub Tabbed_IE_Disposed(sender As Object, e As System.EventArgs) Handles Me.Disposed
        My.Settings.HomePage = HomePage
        My.Settings.Save()
    End Sub

    Private Sub Tabbed_IE_Load(sender As Object, e As System.EventArgs) Handles Me.Load, MyBase.Load
        AddHandler Web1.DocumentCompleted, AddressOf IELoaded
        Me.Anchor = AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top Or AnchorStyles.Bottom
    End Sub

    Private Sub VariableChanged(ByVal NewValue As String) Handles NewURL.VariableChanged
        DirectCast(Me.Tabby.SelectedTab.Controls(0), WebBrowser).Navigate(NewValue)
    End Sub

    Private Sub OpenInNewTabToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OpenInNewTabToolStripMenuItem.Click
        ' create a new webbrowser instance on new tab
        Dim tabpage As New TabPage("This is New Tab")
        Me.Tabby.TabPages.Add(tabpage)
        Dim web1 As New WebBrowser
        web1.Name = "Web1"
        web1.Dock = DockStyle.Fill
        ' re associate contextmenustrip for each webbrowser instance
        web1.IsWebBrowserContextMenuEnabled = False
        web1.ContextMenuStrip = Menu1
        web1.ScriptErrorsSuppressed = True
        Dim target As String
        target = (DirectCast(Me.Tabby.SelectedTab.Controls(0), WebBrowser).Document.ActiveElement.GetAttribute("href"))
        web1.Navigate(target)
        AddHandler web1.NewWindow, AddressOf Web1_NewWindow
        AddHandler web1.DocumentCompleted, AddressOf IELoaded
        tabpage.Controls.Add(web1)
        Me.Tabby.SelectedTab = tabpage
    End Sub

    Private Sub IELoaded(ByVal sender As Object, e As WebBrowserDocumentCompletedEventArgs)
        CurrentBrowser = CType(Tabby.SelectedTab.Controls(0), WebBrowser)
        CurrentDocument = CType(Tabby.SelectedTab.Controls(0), WebBrowser).Document
        DocTitle = CType(Tabby.SelectedTab.Controls(0), WebBrowser).DocumentTitle
        MyLink = CType(Tabby.SelectedTab.Controls(0), WebBrowser).Url.ToString
        If Ele IsNot Nothing Then
            If Ele.TagName = "IMG" Then
                If ImgName IsNot Nothing Then
                    Tabby.SelectedTab.Text = ImgName
                    Tabby.SelectedTab.ToolTipText = Tabby.SelectedTab.Text
                    AddressBar.Text = MyLink
                    ImgName = Nothing
                    Ele = Nothing
                End If
            Else
                Tabby.SelectedTab.Text = DocTitle
                Tabby.SelectedTab.ToolTipText = Tabby.SelectedTab.Text
                AddressBar.Text = MyLink
                Ele = Nothing
            End If
        Else
            If DocTitle IsNot Nothing And MyLink IsNot Nothing Then
                Tabby.SelectedTab.Text = DocTitle
                Tabby.SelectedTab.ToolTipText = Tabby.SelectedTab.Text
                AddressBar.Text = MyLink
            End If
        End If
        CurrentBrowser.Focus()
    End Sub

    Private Sub AddressBar_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles AddressBar.KeyDown
        If e.KeyCode = Keys.Enter Then
            Go()
        End If
    End Sub
    Public Sub Go()
        If AddressBar.Text <> Nothing Then
            DirectCast(Me.Tabby.SelectedTab.Controls(0), WebBrowser).Navigate(AddressBar.Text)
        End If
    End Sub

    Public Sub NewTab()
        ' create a new webbrowser instance on new tab
        Dim web1 As New WebBrowser
        web1.Name = "Web1"
        web1.Dock = DockStyle.Fill
        ' re associate contextmenustrip for each webbrowser instance
        web1.IsWebBrowserContextMenuEnabled = False
        web1.ContextMenuStrip = Menu1
        web1.ScriptErrorsSuppressed = True
        AddHandler web1.NewWindow, AddressOf Web1_NewWindow
        AddHandler web1.DocumentCompleted, AddressOf IELoaded
        Dim tabpage As New TabPage("This is New Tab")
        tabpage.Controls.Add(web1)
        Me.Tabby.TabPages.Add(tabpage)
        Me.Tabby.SelectedTab = tabpage
        DirectCast(Me.Tabby.SelectedTab.Controls(0), WebBrowser).Navigate("www.google.com")
    End Sub

    Private Sub Web1_NewWindow(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Web1.NewWindow
        e.Cancel = True
        ' create a new webbrowser instance on new tab
        Dim tabpage As New TabPage("This is New Tab")
        Me.Tabby.TabPages.Add(tabpage)
        Dim web1 As New WebBrowser
        web1.Name = "Web1"
        web1.Dock = DockStyle.Fill
        ' re associate contextmenustrip for each webbrowser instance
        web1.IsWebBrowserContextMenuEnabled = False
        web1.ContextMenuStrip = Menu1
        web1.ScriptErrorsSuppressed = True
        web1.Navigate(Linx)
        AddHandler web1.NewWindow, AddressOf Web1_NewWindow
        AddHandler web1.DocumentCompleted, AddressOf IELoaded
        tabpage.Controls.Add(web1)
        Me.Tabby.SelectedTab = tabpage
    End Sub

    Private Sub Tabby_DoubleClick(sender As Object, e As System.EventArgs) Handles Tabby.DoubleClick
        Tabby.SelectedTab.Dispose()

    End Sub

    Private Sub Tabby_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles Tabby.SelectedIndexChanged
        If Tabby.TabCount > 0 Then
            CurrentBrowser = CType(Tabby.SelectedTab.Controls(0), WebBrowser)
            CurrentDocument = CType(Tabby.SelectedTab.Controls(0), WebBrowser).Document
            Dim Target As Uri = CType(Tabby.SelectedTab.Controls(0), WebBrowser).Url
            If Target IsNot Nothing Then
                AddressBar.Text = Target.ToString
                MyURL = Target.ToString
            End If
        End If
    End Sub

    Private Sub SaveImageAsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SaveImageToolStripMenuItem.Click
        Dim ImageSource As String = Ele.GetAttribute("src")
        MySource = ImageSource
        Dim SavePath As String = InputBox("Please enter where you want to save this image, including the file name and extension")
        My.Computer.Network.DownloadFile(ImageSource, SavePath)
    End Sub

    Private Sub ViewImageToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles ViewImageToolStripMenuItem.Click
        ' create a new webbrowser instance on new tab
        Dim tabpage As New TabPage("This is New Tab")
        Me.Tabby.TabPages.Add(tabpage)
        Dim web1 As New WebBrowser
        web1.Name = "Web1"
        web1.Dock = DockStyle.Fill
        ' re associate contextmenustrip for each webbrowser instance
        web1.IsWebBrowserContextMenuEnabled = False
        web1.ContextMenuStrip = Menu1
        Dim target As String = Ele.GetAttribute("src")
        ImgName = target.Substring(target.LastIndexOf("/") + 1)
        web1.Navigate(target)
        AddHandler web1.NewWindow, AddressOf Web1_NewWindow
        AddHandler web1.DocumentCompleted, AddressOf IELoaded
        tabpage.Controls.Add(web1)
        Me.Tabby.SelectedTab = tabpage
    End Sub

    Private Sub hover(ByVal sender As Object, ByVal e As HtmlElementEventArgs) Handles CurrentDocument.MouseMove
        MousePoint = e.ClientMousePosition
        Ment = CurrentBrowser.Document.GetElementFromPoint(MousePoint)
        Linx = Ment.GetAttribute("href")
        Sauce = Ment.GetAttribute("src")
        'Ele = CurrentBrowser.Document.GetElementFromPoint(MousePoint)
        'Dim target As String = Ele.GetAttribute("src")
        'AddressBar.Text = e.ClientMousePosition.ToString & Environment.NewLine & target.ToString
    End Sub

    Private Sub Menu1_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Menu1.Opening

        Ele = CurrentBrowser.Document.GetElementFromPoint(MousePoint)

        'If Ele.TagName = "A" Then
        '    LinkToolStripMenuItem.Visible = True
        'Else
        '    LinkToolStripMenuItem.Visible = False
        'End If
        'If Ele.TagName = "IMG" Then
        '    ImageToolStripMenuItem.Visible = True
        'Else
        '    ImageToolStripMenuItem.Visible = False
        'End If
    End Sub

    Private Sub CopyLinkLocationToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CopyLinkLocationToolStripMenuItem.Click
        Dim target As String = Ele.GetAttribute("href")
        Clipboard.SetText(target)
    End Sub

    Private Sub CopyImageLocationToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles CopyImageLocationToolStripMenuItem.Click
        Dim target As String = Ele.GetAttribute("src")
        Clipboard.SetText(target)
    End Sub

    Private Sub GoBackToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles GoBackToolStripMenuItem.Click
        GoBack()
    End Sub
    Public Sub GoBack()
        If CType(Tabby.SelectedTab.Controls(0), WebBrowser).CanGoBack = True Then
            CType(Tabby.SelectedTab.Controls(0), WebBrowser).GoBack()
        End If
    End Sub

    Private Sub GoForwardToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles GoForwardToolStripMenuItem.Click
        Fwd()
    End Sub
    Public Sub Fwd()
        If CType(Tabby.SelectedTab.Controls(0), WebBrowser).CanGoForward = True Then
            CType(Tabby.SelectedTab.Controls(0), WebBrowser).GoForward()
        End If
    End Sub

    Private Sub RefreshToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles RefreshToolStripMenuItem.Click
        Refresher()
    End Sub
    Public Sub Refresher()
        CType(Tabby.SelectedTab.Controls(0), WebBrowser).Refresh()
    End Sub

    Private Sub HomeToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles HomeToolStripMenuItem.Click
        GoHome()
    End Sub
    Public Sub GoHome()
        If Not My.Settings.HomePage = Nothing Then
            CType(Tabby.SelectedTab.Controls(0), WebBrowser).Navigate(My.Settings.HomePage)
        Else
            CType(Tabby.SelectedTab.Controls(0), WebBrowser).Navigate("www.google.com")
        End If
    End Sub

    Private Sub ChooseNewHomeToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ChooseNewHomeToolStripMenuItem.Click
        HomePage = InputBox("Enter the URL you want to be your new home page.")
        My.Settings.HomePage = HomePage
        My.Settings.Save()
    End Sub

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
        CType(Tabby.SelectedTab.Controls(0), WebBrowser).Document.ExecCommand("COPY", False, Nothing)
        Clipboard.SetText(Trim(Clipboard.GetText))

    End Sub

    Private Sub PasteToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles PasteToolStripMenuItem.Click
        CType(Tabby.SelectedTab.Controls(0), WebBrowser).Document.ExecCommand("PASTE", False, Nothing)
    End Sub

    Private Sub OpenLinkInDefaultBrowserToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OpenLinkInDefaultBrowserToolStripMenuItem.Click
        System.Windows.Forms.Cursor.Current = Cursors.AppStarting
        Dim target As String
        target = (DirectCast(Me.Tabby.SelectedTab.Controls(0), WebBrowser).Document.ActiveElement.GetAttribute("href"))
        BrowserExec.LaunchNewBrowser(target)
        System.Windows.Forms.Cursor.Current = Cursors.Default
    End Sub


End Class


Imports System.IO, System.Net, System.Drawing
Imports System.Windows.Forms

Public Class Class1

End Class

Public Class Tabbed_Browers

    Public Shared Function GetWebImage(ByVal ImageURL As String) As Image
        Dim objImage As MemoryStream
        Dim objwebClient As WebClient
        Dim sURL As String = Trim(ImageURL)
        Dim oImage As Image
        Try
            If Not sURL.ToLower().StartsWith("http://") _
            Then sURL = "http://" & sURL
            objwebClient = New WebClient
            objImage = New  _
            MemoryStream(objwebClient.DownloadData(sURL))
            oImage = System.Drawing.Image.FromStream(objImage)
            Return oImage
        Catch ex As Exception
            'Return something, an error or no image as default
            Return Nothing
        End Try
    End Function

End Class

Public Class myVar
    Public mValue As String
    Public Event VariableChanged(ByVal mvalue As String)
    Public Property Variable() As String
        Get
            Variable = mValue
        End Get
        Set(ByVal value As String)
            mValue = value
            RaiseEvent VariableChanged(mValue)
        End Set
    End Property
End Class

Module BrowserExec

    Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Integer

    Public Sub LaunchNewBrowser(ByVal strUrl As String)

        Try
            'CREATE SOME VARIABLES
            Dim BrowserExec As String = New String(" "c, 255)
            Dim FileName As String
            Dim RetVal As Long
            Dim FileNumber As Integer
            Dim SWriter As StreamWriter
            'CREATE A TEMP HTM FILE SO WE CAN FIND OUT WHAT BROWSER IS
            'THE DEFAULT ONE ON THE SYSTEM
            FileName = Application.StartupPath & "\temp.htm"
            SWriter = File.CreateText(FileName)
            SWriter.Write("<html></html>")
            SWriter.Close()
            'CALL THE API TO FIND THE EXE ASSOCIATED WITH HTM FILES
            RetVal = FindExecutable(FileName, vbNullString, BrowserExec)
            BrowserExec = BrowserExec.Trim
            'IF WE GET ONE, LAUNCH THE URL IN THE NEW INSTANCE OF THE BROWSER
            If RetVal <= 32 Or BrowserExec = String.Empty Then
                MessageBox.Show("Could not find a valid web browser")
            Else
                Process.Start(BrowserExec, strUrl)
            End If
            'DELETE THAT PESKY TEMP FILE
            File.Delete(FileName)
        Catch ex As Exception
            'IF ANYTHING GOES WRONG, LET PEOPLE KNOW WHO'S FAULT IT IS
            MessageBox.Show("An error has occured cause kleinma obviously can't write good code" & ControlChars.CrLf & ex.Message)
        End Try

    End Sub
End Module


https://sites.google.com/site/amerigoware/Home/Tabbed%20Browser.zip?attredirects=0&d=1[^]
GeneralRe: Where's the download link? Pin
rspercy655-Jul-13 10:37
rspercy655-Jul-13 10:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.