I needed a multi-tabbed web browser for one of my projects. I searched around on the internet for one that was already made to save time but all I found was custom controls that were not, from a developer point, intuitive and easy to implement in an existing solution. Most of them were bloated with junk. So I decided to make my own.
Here's an easy way to create a simple, yet clean, web browser that supports tabs.
Sections:
I. Building the Form
II. Making Our Web Browser Navigate
III. Creating the Custom WebBrowser Control
IV. Making Our Browser Add New Tabs
V. Finishing Touches
I. Building the Form
Add some navigation buttons, a combo box, and a tab control. Instead of using text for my buttons, I will take a better approach and use images. The icon set I'm using is FamFamFam's Silk Icon Set. I will list the individual icons used at the end of this tutorial.[1] If you want a more professional feel to your web browser, set the combobox's AutoCompleteSource to HistoryList and AutoCompleteMode to Suggest. When you add your tab control you're going to want to remove the two TabPages that are automatically added to your tab control.
Here's what my form looks like so far:

II. Making Our Web Browser Navigate
The best way to keep our web browser's linked to each tab page, we will use the Control.Tag property. If you're unfamiliar with the Control.Tag property, you may want to read this. When we press the Back button on our browser, how will our program know which web browser to navigate back. We will tell it which one to invoke based on the selected TabPage's Tag property. Here is our code for the navigation.
Private Sub btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
Dim WB As CustomBrowser = Me.TabControl1.SelectedTab.Tag
WB.GoBack()
End Sub
Private Sub btnForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForward.Click
Dim WB As CustomBrowser = Me.TabControl1.SelectedTab.Tag
WB.GoForward()
End Sub
Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
Dim WB As CustomBrowser = Me.TabControl1.SelectedTab.Tag
WB.Refresh()
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
Dim WB As CustomBrowser = Me.TabControl1.SelectedTab.Tag
WB.Stop()
End Sub
Private Sub btnAddTab_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddTab.Click
AddTab("about:blank", TabControl1)
End Sub
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
Dim WB As CustomBrowser = Me.TabControl1.SelectedTab.Tag
WB.Navigate(Me.cbUrl.Text)
End SubIgnore the AddTab(String, TabControl) method for now. We will get back to that.
III. Creating the Custom Web Browser Control
Since we're going to be creating new web browsers on the fly, we're going to have to create a custom web browser just to handle two things; suppress script errors and to change the TabPage's text to the web browser's document title. Just create a new class but make sure you inherit the System.Windows.Forms.WebBrowser class so it's basically like an extended web browser control. Here's the code for our custom web browser.
Public Class CustomBrowser Inherits WebBrowser Private Sub DocCompleted() Handles Me.DocumentCompleted Dim TP As TabPage = Me.Tag If Me.DocumentTitle.Length > 10 Then TP.Text = Me.DocumentTitle.Substring(0, 9) & "..." Else TP.Text = Me.DocumentTitle End If Me.ScriptErrorsSuppressed = True End Sub End Class
IV. Making Our Browser Add New Tabs
This is actually a lot easier than you may think. We will just add a new TabPage to the TabControl, create a new instance of our CustomBrowser control, tag them up, and navigate the newly created CustomBrowser control. Here's our AddTab(String, TabControl) method.
Public Sub AddTab(ByRef URL As String, ByRef TabControl As TabControl) Dim NewBrowser As New CustomBrowser Dim NewTab As New TabPage NewBrowser.Tag = NewTab NewTab.Tag = NewBrowser TabControl.TabPages.Add(NewTab) NewTab.Controls.Add(NewBrowser) NewBrowser.Dock = DockStyle.Fill NewBrowser.Navigate(URL) End Sub
V. Finishing Touches
If you remember, we removed all of the default tabs in our TabControl. On you're Form's load event, you're going to have to call the AddTab(String, TabControl) function.
The final project is attached as a zip file below.
[1] Images used: arrow_back.png, arrow_forward.png, arrow_refresh.png, stop.png, tab_add.png, accept.png.
Attached File(s)
-
VB_Tabbed_Web_Browser.zip (138.52K)
Number of downloads: 1192

Sign In
Register
Help



MultiQuote




