New tabs for a web browser

borter

Member
Joined
Jul 12, 2013
Messages
7
Programming Experience
Beginner
I am making a web browser, and I am trying to add a new tab by clicking a menu strip item;However, I am having trouble with the tabs themselves. The first tab works normally, but when I add a new tab, it adds the tab but it also gets rid of the web browser in the previous tab.I do not plan to use the tab control to browse the web, I want to have a web browser inside of each new tab.
Here is my code for the original tab page.(In case it is important)
Code:
    Function addtab()
        Dim myTabPage As New TabPage()
        AddHandler theweb.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf Run_Me_On_Load)
        myTabPage.Text = "New Tab" & (TabControl1.TabPages.Count + 1)
        TabControl1.TabPages.Add(myTabPage)
        theweb.DocumentText = My.Resources._New
        theweb.Parent = myTabPage
        theweb.Visible = True
        theweb.Dock = DockStyle.Fill
        Return True
        Me.SetStyle(ControlStyles.DoubleBuffer, True)
    End Function
And here is my code for the menu strip item.
Code:
Dim theweb As New WebBrowser
        Dim myTabPage As New TabPage()
        TabControl1.TabPages.Add(myTabPage)
        myTabPage.Text = "New Tab"
        TabControl1.SelectTab(TabControl1.SelectedIndex)
        theweb.DocumentText = My.Resources._New
        theweb.Parent = myTabPage
        theweb.Visible = True
        theweb.Dock = DockStyle.Fill

How could I make it so that when I click the menu strip item it puts a new web browser inside of the new tab it creates?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,171
Location
Sydney, Australia
Programming Experience
10+
I'm not quite sure why you have two sets of code to do the same thing. Adding a new tab containing a web browser is, or at least should be, exactly the same whether it's the first tab or not. Just write a method that creates a new TabPage, creates a new WebBrowser and adds it to the TabPage and then adds the TabPage to the TabControl. You then call that same method when you startup to display the first tab and also from the menu item's Click event handler to add subsequent tabs.
 

borter

Member
Joined
Jul 12, 2013
Messages
7
Programming Experience
Beginner
It still did not work, when I click the menu item to add a new tab it does create a new tab, but it does not create a new web browser for that tab.
This is what I have, am I just making an idiotic error?
Code:
[COLOR=#00008B]Public Class Form1
    Dim theweb As New WebBrowser()
'this is so that I can specify it with the forward and backward buttons later on'
Function[/COLOR] addtab()
    [COLOR=#00008B]Dim[/COLOR] myTabPage [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]New[/COLOR] TabPage()
    [COLOR=#00008B]AddHandler[/COLOR] theweb.DocumentCompleted, [COLOR=#00008B]New[/COLOR] WebBrowserDocumentCompletedEventHandler([COLOR=#00008B]AddressOf[/COLOR] Run_Me_On_Load)
    myTabPage.Text = [COLOR=#800000]"New Tab"[/COLOR] & (TabControl1.TabPages.Count + [COLOR=#800000]1[/COLOR])
    TabControl1.TabPages.Add(myTabPage)
    theweb.DocumentText = my.resources.new
    theweb.Parent = myTabPage
    theweb.Visible = [COLOR=#800000]True[/COLOR]
    theweb.Dock = DockStyle.Fill
    [COLOR=#00008B]Return[/COLOR] [COLOR=#800000]True[/COLOR]
[COLOR=#00008B]End[/COLOR] [COLOR=#00008B]Function


    Private Sub NewPocketDimensionToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewPocketDimensionToolStripMenuItem.Click
        addtab()
        Me.SetStyle(ControlStyles.DoubleBuffer, True)
    End Sub
[/COLOR]


 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,171
Location
Sydney, Australia
Programming Experience
10+
Of course it doesn't create a new WebBrowser. The only place you're creating a new WebBrowser is outside of any method, so it only gets done once. If you want to create a new WebBrowser each time you call 'addtab' then you need to create the new WebBrowser inside the 'addtab' method, just as you create a new TabPage inside the 'addtab' method.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,171
Location
Sydney, Australia
Programming Experience
10+
Also, why would you be double-buffering the form every time you add a tab? That is something that you do once when the form is first created. It's then double-buffered it's entire lifetime.
 
Top Bottom