How to make a tab browser

How to let my application respond to every tab user has selected

Page 1 of 1

2 Replies - 5792 Views - Last Post: 13 October 2008 - 09:43 AM Rate Topic: -----

#1 rakyomin  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 77
  • Joined: 12-September 08

How to make a tab browser

Posted 12 October 2008 - 08:18 PM

I am making a tab browser, I know how to get the first tab to navigate according to user's desired url.

There is a tab which contain a webbrowser control known as WebBrowser1 with a dockstyle.fill within the Tab Control.
My form is designed to appear as only one tab. User can choose to create a new tab from the MenuStrip Control.

Below is my code:

Public Class frmWebSurfer

    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
        WebBrowser1.Navigate(cbxUrl.Text)

    End Sub


    Private Sub BackToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackToolStripMenuItem.Click
        WebBrowser1.GoBack()
    End Sub

    Private Sub ForwardToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ForwardToolStripMenuItem.Click
        WebBrowser1.GoForward()
    End Sub

    Private Sub RefreshToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshToolStripMenuItem.Click
        WebBrowser1.Refresh()

    End Sub

    Private Sub StopToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopToolStripMenuItem.Click
        WebBrowser1.Stop()
    End Sub

    
    Private Sub cbxUrl_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cbxUrl.KeyDown
        If e.KeyCode = Keys.Enter Then
            e.SuppressKeyPress = True
            WebBrowser1.Navigate(cbxUrl.Text)
        End If
    End Sub

    Private Sub NewTabToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewTabToolStripMenuItem.Click
        'create a new tab
        Dim newTabPage As New TabPage
        Dim newWebBrowser As New WebBrowser

        newTabPage.Text = "TabPage" & tabBrowserFace.TabPages.Count + 1
        'add browser to the tab control
        newTabPage.Controls.Add(newWebBrowser)
        newWebBrowser.Dock = DockStyle.Fill
        newWebBrowser.Navigate(cbxUrl.Text)
        'add the new tab
        Me.tabBrowserFace.Controls.Add(newTabPage)

    End Sub
End Class



As you can see WebBrowser1.Navigate can only respond to tabpage1, but not subsequent page user chooses to click.
If I add newWebBrowser.Navigate(cbxUrl.Text) in btnGo.Click event it will only respond to new tabpages user created and not to the first tabpage upon the application opens. What should I write btnGo.Click so that default tabpage1 and subsequent user created tabpages will respond to user's desired url?

Is This A Good Question/Topic? 0
  • +

Replies To: How to make a tab browser

#2 rakyomin  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 77
  • Joined: 12-September 08

Re: How to make a tab browser

Posted 12 October 2008 - 08:53 PM

I rewrote my btnGo.click event as below:

Private newWebBrowser As WebBrowser

    
    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click

        If tabBrowserFace.TabPages.Count = 0 Then
            WebBrowser1.Navigate(cbxUrl.Text)
        Else
            newWebBrowser.Navigate(cbxUrl.Text)
        End If



But VB.net gave error "Object reference not set to an instance of an object." which is not true.. because in NewTabToolStripMenuItem.Click event: there is a new instance created for WebBrowser..
Was This Post Helpful? 0
  • +
  • -

#27 rakyomin  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 77
  • Joined: 12-September 08

Re: How to make a tab browser

Posted 13 October 2008 - 09:43 AM

ok since no one replies, i managed to solve this issue.

for the benefit of others facing the same problem here is the code sample:

CType(tabControl.SelectedTab.Controls.Item(0), WebBrowser).Navigate(cbxUrl.Text)



by changing the type of the item selected of the tab control to webbrowser, vb.net can recognise which tab is selected and act according to what user has input in the url text box.
Was This Post Helpful? 1

Page 1 of 1