Ahmed H
Ahmed H

Reputation: 21

How Do I Open a New Webbrowser Tab in Vb.net application

I have a Form1 with button1 and a webbrowser1. When I click on button1, I want to open a new web browser tab in the same form, not in Firefox or Internet Explorer or Chrome.

I tried using TabControl but am not sure how that works since it does not resize and its kind of annoying. I just want to open a new tab with web browser in the form.

Public Class Form1
  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim wb As New WebBrowser
    wb.Navigate("www.google.com")
    Dim tab As New TabPage("Title")
    tab.Controls.Add(wb)
    TabControl1.TabPages.Add(tab)
    TabControl1.SelectedTab = tab
    tab.Size = New System.Drawing.Size(280, 174)
  End Sub
End Class

Upvotes: 1

Views: 15000

Answers (3)

Trevor
Trevor

Reputation: 8004

This should work:

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim tabpage As New TabPage
    tabpage.Text = "New Tab"

    TabControl1.TabPages.Add(tabpage)

    Dim webBrowser As New WebBrowser
    TabControl1.SelectedTab = tabpage
    tabpage.Controls.Add(webBrowser)
    webBrowser.Dock = DockStyle.Fill
    webBrowser.Navigate("http://www.stackoverflow.com")
End Sub

Upvotes: 0

Parimal Raj
Parimal Raj

Reputation: 20575

To add a new Tabbed browser, first u need to add a new Tab to your existing TabControl, once the new Tab is added, then you need to add a new browser control into the created Tab

Private Sub btnAddTab_Click(sender As Object, e As EventArgs)
    Dim page As New TabPage(String.Format("Tab # {0}", tabControl1.TabPages.Count + 1))
    tabControl1.TabPages.Add(page)
    Dim browser As New WebBrowser()
    page.Controls.Add(browser)
    browser.Dock = DockStyle.Fill
    browser.Navigate(New Uri("http://www.google.co.in"))

End Sub

Upvotes: 2

gunakkoc
gunakkoc

Reputation: 1069

Create your own TabPage so that you can handle events and controls easly:

Public Class WBTab
    Inherits TabPage 'it actually is a tabpage
    Public WithEvents WB As New WebBrowser 'that has a single webbrowser in it
    Sub New(ByVal URL As String) 'when the page is created, show it and load the URL
        WB.Dock = DockStyle.Fill
        Me.Controls.Add(WB)
        WB.Navigate(URL)
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WB.DocumentCompleted
        Me.Text = WB.DocumentTitle 'when the page is loaded you may now show its title in your tab.
    End Sub

    Private Sub WB_Navigating(sender As Object, e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WB.Navigating
        Me.Text = e.Url.ToString
    End Sub
End Class

Now it is ready to be used:

Dim google As New WBTab("google.com") 'create a new tab with URL
TabControl1.TabPages.Add(google) 'show it

Upvotes: 0

Related Questions