locked
Tabbed Web Browsing RRS feed

  • Question

  •   hi everyone at the moment I'm trying to make a web browser which supports tabbed web browsing I've made it so that you can click a button to add new tabs but I'm not sure how to make the address text box control the tab which is currently being viewed, at the moment it's only able to control the first tab.

      How do I get it to control the tab which is currently being viewed? and also how do I get the web page's name to be shown on the tab?

       my code so far :

     

    Public Class Browser

    'Creating an instance of the Navigate class

     

    Dim NavigateWindow As New Navigate

     

     

    Private Sub Browser_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click

    End Sub

     

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ' Disabling both navigation buttons in the Icons tool strip

     

    Me.tsbBack.Enabled = False

     

    Me.tsbForward.Enabled = False

     

    Me.lblApplicationStatus.Text = "Ready"

     

    End Sub

     

    Private Sub HelpToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

     

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    AboutBox1.ShowDialog()

    End Sub

     

    Private Sub AboutBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

     

    Private Sub AboutToolStripMenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem1.Click

    End Sub

     

    Private Sub AboutToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem1.Click

    AboutBox1.ShowDialog()

    End Sub

    Private Sub tsbBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbBack.Click

    myBrowser.GoBack()

    End Sub

     

    Private Sub tsbForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbForward.Click

    myBrowser.GoForward()

    End Sub

     

    Private Sub tsbStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbStop.Click

    myBrowser.Stop()

    End Sub

     

    Private Sub tsbRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbRefresh.Click

    myBrowser.Refresh()

    End Sub

     

    Private Sub tsbHome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbHome.Click

    myBrowser.GoHome()

    End Sub

     

    Private Sub tsbSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbSearch.Click

    myBrowser.GoSearch()

    End Sub

     

    Private Sub myBrowser_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)

    'Get Application title using the My namespace

     

    If My.Application.Info.Title <> "" Then

     

    Me.Text = My.Application.Info.Title + " - " + e.Url.Host.ToString()

    Else

     

    'If the applicaton title is missing,

     

    'use the application name, without the extension

     

    Me.Text = System.IO.Path.GetFileNameWithoutExtension(My.Application.Info.AssemblyName) + " - " + e.Url.Host.ToString()

    End If

     

    Me.lblApplicationStatus.Text = "Ready"

     

    End Sub

     

    Private Sub myBrowser_Navigating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs)

    ' Add the code to enable or disable whenever there are URLs

     

    'in the browsing session's history

     

    If myBrowser.CanGoBack Then

     

    tsbBack.Enabled = True

     

    Else

     

    tsbBack.Enabled = False

     

    End If

     

    If myBrowser.CanGoForward Then

     

    tsbForward.Enabled = True

     

    Else

     

    tsbForward.Enabled = False

     

    End If

     

    ' Modifying the label in the status strip with the URL entered by the user

     

    Me.lblApplicationStatus.Text = "Opening page: " + e.Url.Host.ToString()

    End Sub

     

    Private Sub myBrowser_ProgressChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs)

    ' The CurrentProgress variable from the raised event

     

    ' gives you the current number of bytes already downloaded

     

    ' while the MaximumProgress is the total number of bytes

     

    ' to be downloaded

     

    If e.CurrentProgress < e.MaximumProgress Then

     

    ' Check if the current progress in the progress bar

     

    ' is >= to the maximum if yes reset with the min

     

    If pbStatus.Value >= pbStatus.Maximum Then

     

    pbStatus.Value = pbStatus.Minimum

    Else

     

    ' Just increase the progress bar

     

    pbStatus.PerformStep()

    End If

     

    Else

     

    'When the document is fully downloaded

     

    'reset the progress bar to the min (0)

     

    pbStatus.Value = pbStatus.Minimum

    End If

     

    End Sub

     

    Private Sub tstbUrl_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles tstbUrl.KeyUp

    'e is of type KeyEventArgs and contains all the

     

    'information that triggered the event. The KeyCode

     

    'is one those information.

     

    If e.KeyCode = Keys.Enter Then

     

    Me.NavigateToUrl(tstbUrl.Text)

    End If

     

    End Sub

     

    Private Sub NavigateToUrl(ByVal Url As String)

    Me.myBrowser.Navigate(Url)

    End Sub

     

    Private Sub tsbGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbGo.Click

    Me.NavigateToUrl(tstbUrl.Text)

    End Sub

     

    Private Sub NavigateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NavigateToolStripMenuItem.Click

    If (NavigateWindow.ShowDialog() = Windows.Forms.DialogResult.OK) Then

     

    Me.NavigateToUrl(NavigateWindow.txtUrl.Text)

    End If

     

    NavigateWindow.txtUrl.Text = ""

     

    End Sub

     

     

    Private Sub SToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

     

    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Dim x As New TabPage

    With x

    Dim x1 As New WebBrowser

    x1.Dock = DockStyle.Fill

    x1.Navigate(New Uri("http://www.microsoft.com"))

    .Controls.Add(x1)

    End With

     

    Me.TabControl1.TabPages.Add(x)

    End Sub

     

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim x As New TabPage

    With x

    Dim x1 As New WebBrowser

    x1.Dock = DockStyle.Fill

    x1.Navigate(New Uri("http://www.microsoft.com"))

    .Controls.Add(x1)

    End With

     

    Me.TabControl1.TabPages.Add(x)

    End Sub

    End Class

    -------------------------------------------------------CODE END------------------------------------------------------------------------

     

    The first ab is called myBrowser if that matters.

    Do i have to do the same as i have for myBrowser for each tab? or is it easier than that?

     

     

    THX IN ADVANCE :)

    Thursday, January 25, 2007 6:46 PM

Answers

  •  

    Pay close attention to every aspect of this code. There is a lot of subtlety in it.

     

    This is a brief demo of how to do this. It doesn't do everything a browser class needs to do but it illustrates a framework. 

    Public Class Form1

     

        Private Sub Button1_Click_1(ByVal sender As System.Object, _

                                               ByVal e As System.EventArgs) _

                                               Handles cbGo.Click

            Dim tp As New Browser

            tp.Tag = tp

            TabControl1.TabPages.Add(tp)

            TabControl1.SelectedTab = _

                         TabControl1.TabPages(TabControl1.TabPages.Count - 1)

     

        End Sub

     

        Private Sub cbExit_Click(ByVal sender As Object, _

                                     ByVal e As System.EventArgs) _

                                     Handles cbExit.Click

     

            if TabControl1.TabPages.Count = 1 then exit sub

     

            ' For purpose of illustration,delete the last tab

     

            Dim tp As Browser = _

                       TabControl1.TabPages(TabControl1.TabPages.Count - 1)

            tp.Dispose1()

        End Sub

    End Class

     

     

    Public Class Browser

        Inherits Tabpage

        Private gbTB As New GroupBox : Private gbwb As New GroupBox

        Protected Friend WithEvents tbAddressbar As New TextBox

        Dim wb As New Webbrowser

     

        Public Sub New()

            gbwb.Controls.Add(wb) : wb.Location = New Point(0, 0)

            gbwb.Location = New Point(0, 0)

            gbwb.Padding = New Padding(0)

            gbwb.Size = New Size(Form1.TabControl1.Width, Form1.TabControl1.Height - 100)

            wb.Size = gbwb.Size

            gbTB.Controls.Add(tbAddressbar)

            Me.BackColor = Color.Blue

            tbAddressbar.Location = New Point(5, 35)

            Me.Controls.Add(gbTB)

            gbTB.Size = New Size(Form1.TabControl1.Width - 10, 80)

            gbTB.Location = New Point(5, Form1.TabControl1.Height - gbTB.Height - 20)

            tbAddressbar.Width = gbTB.Width - 10

            tbAddressbar.Location = New Point(5, 20)

            Me.Controls.Add(gbwb)

            wb.Navigate("www.google.com")

        End Sub

     

     

     

        Private Sub tbAddressbar_KeyPress(ByVal sender As Object, _

                    ByVal e As System.Windows.Forms.KeyPressEventArgs) _

                    Handles tbAddressbar.KeyPress

            If e.KeyChar = Chr(Keys.Enter) Then

                wb.Navigate(tbAddressbar.Text)

                e.Handled = True

            End If

        End Sub

     

        Public Sub Dispose1()

            tbAddressbar.Dispose()

            gbTB.Dispose()

            wb.Dispose()

            gbwb.Dispose()

            Me.Dispose()

        End Sub

     

    End Class

     

     

     

     

     

     

    Friday, January 26, 2007 9:54 PM

All replies

  • Each tab should keep track of the web page it's viewing (perhaps the URL property of the contained browser?). Each time you click a tab, set the text box text to the URL of the browser object in that tab.

    Thursday, January 25, 2007 10:00 PM
  •  

    Think of a tab as a class..... There is not one adress bar, there are N address bars with one showing at a time.

    Each class has it's own address bar wih it's own storage. At least that how mine works.

    Thursday, January 25, 2007 10:06 PM
  •  

     

    Public Class Form1

    Private Sub Button1_Click_1(ByVal sender As System.Object, _
                                          
    ByVal e As System.EventArgs) Handles Button1.Click

    Tabcontrol1.Tabpages.add(new Browser)

    End Sub

    End Class

     

    Public Class Browser
       Inherits Tabpage
     

      Dim wb As New Webbrowser

    Public Sub New()

               me.controls.add(wb)
              
    wb.Dock = DockStyle.Fill

    End Sub

    End Class


     

    That's all there is to it on the surface. Actually you don't want to navigate anywhere from browser creation although you could

     

     

    Public Class Form1

    Private Sub Button1_Click_1(ByVal sender As System.Object, _
                                          
    ByVal e As System.EventArgs) Handles Button1.Click

    Tabcontrol1.Tabpages.add(new Browser("www.microsoft.com"))

              End Sub
    End Class

    Public Class Browser
       Inherits Tabpage
     

      Dim wb As New Webbrowser

    Public Sub New(byval URL as string)

               me.controls.add(wb)
              
    wb.Dock = DockStyle.Fill

               wb.nagivate(URL)

    End Sub

    End Class

    Friday, January 26, 2007 4:14 AM
  • This is a realy nice code but how can i navigate a specific webbrowser and get the url out of a textbox?
    Friday, January 26, 2007 1:07 PM
  •  

    I need to know more.

    From what point are you referring? Where is the textbox? is there just one associated with the main form? Is there one textbox per browser? Getting information into and out of  this class is not difficult at all with properties.

     

    I've written a nice tabbed webbrowser and you can download it you like to play with. To get some ideas.

    Friday, January 26, 2007 1:41 PM
  •  Cool, 1)Where can i download it?

    2) The text box is on the top of the form and its the only one on the form

    Friday, January 26, 2007 4:44 PM
  • http://www.freewebs.com/reneecc/

    I have an intuitive feeling that what you're planning is a bit of a design error. I can't understand why if you have multiple browsers, toy want one textbox. On my browser it appears that there is only one, but actually the textbox belongs to each class containing the WebBrowser.

    I think each class should pretty autonomous and that you don't want a whole lot of correspondence between the class containing the webBrowser and of part of the form.

    Friday, January 26, 2007 4:53 PM
  •  How do i get the textbox to change which tab it's controlling, would it be like If tab1.Visible then me.comntrol  (example i know THAT wouldnt work)

    Friday, January 26, 2007 5:19 PM
  • I think Renees idea is a good one: a separate 'address' textbox for each tab (and since she's actually ceated such a beastie, I think her advice is probably pretty spot on). I would presume that each box is visible only for the required tab. It seems to be fairly evident in Firefox, but IE7 is very frustrating since it erases what you may have typed in and replaces it with the actual web page address when you 'tab' back to that tab - wheras firefox will keep whatever you have typed; a simple hit of the escape key brings baclk the actual page. A nice feature.

    Friday, January 26, 2007 6:17 PM
  • So what do i do, have an address textbox IN each tab, or multiple placed on top of each other with a different one appearing on each tab. (If the latter then how??)
    Friday, January 26, 2007 7:05 PM
  • Have a look and see how Renee does it.

    If you want the address in each tab, go ahead and do that - It looks pretty cool doing it that way. If you want a 'single' address bar, there are two things need to do (Note that this is just one way to achieve what you want):

    1. Create and destroy texboxes as you create and destroy tabs.

    2. Make those text boxes visible/invisible based on the tab selected.

    Okay, there are three: three things you need to do:

    3. position the text boxes in the same location.

    This is positioning the text boxes on top of one another, but only one is visible. An alternative would be to control the ZOrder, bringing the required Textbox to the top.

    Friday, January 26, 2007 7:17 PM
  •   How do I make/destroy textboxes as i create tabs and make them vidible/invisible, I've tried Button1 Click tsNavigation.TextBox.Add but that doesnt work

     

    Friday, January 26, 2007 7:30 PM
  •  

    What you have to say SJ is all correct and it's all doable but it becomes a mess resulting from a poor design decision.

    You see he's then going to want to other things from another place and he'll need more control's and more properties and more synchronization. It will become unmanagable very quickly.

    Each tab should be a fairly autonomous class.

    To dispose, you make up your own dispose method that disposes of all controls. Even if you didn't do that, garbage collection will dispose of them after the class is destroyed.

    Public Class Browser
       Inherits Tabpage
     

      Dim wb As New Webbrowser

    Public Sub New(byval URL as string)

               me.controls.add(wb)
              
    wb.Dock = DockStyle.Fill

               wb.nagivate(URL)

    End Sub

    Public Sub Dispose

       Wb.dispose

    'etc, etc,etc

    end sub

    End Class

    Friday, January 26, 2007 8:37 PM
  •  

    Pay close attention to every aspect of this code. There is a lot of subtlety in it.

     

    This is a brief demo of how to do this. It doesn't do everything a browser class needs to do but it illustrates a framework. 

    Public Class Form1

     

        Private Sub Button1_Click_1(ByVal sender As System.Object, _

                                               ByVal e As System.EventArgs) _

                                               Handles cbGo.Click

            Dim tp As New Browser

            tp.Tag = tp

            TabControl1.TabPages.Add(tp)

            TabControl1.SelectedTab = _

                         TabControl1.TabPages(TabControl1.TabPages.Count - 1)

     

        End Sub

     

        Private Sub cbExit_Click(ByVal sender As Object, _

                                     ByVal e As System.EventArgs) _

                                     Handles cbExit.Click

     

            if TabControl1.TabPages.Count = 1 then exit sub

     

            ' For purpose of illustration,delete the last tab

     

            Dim tp As Browser = _

                       TabControl1.TabPages(TabControl1.TabPages.Count - 1)

            tp.Dispose1()

        End Sub

    End Class

     

     

    Public Class Browser

        Inherits Tabpage

        Private gbTB As New GroupBox : Private gbwb As New GroupBox

        Protected Friend WithEvents tbAddressbar As New TextBox

        Dim wb As New Webbrowser

     

        Public Sub New()

            gbwb.Controls.Add(wb) : wb.Location = New Point(0, 0)

            gbwb.Location = New Point(0, 0)

            gbwb.Padding = New Padding(0)

            gbwb.Size = New Size(Form1.TabControl1.Width, Form1.TabControl1.Height - 100)

            wb.Size = gbwb.Size

            gbTB.Controls.Add(tbAddressbar)

            Me.BackColor = Color.Blue

            tbAddressbar.Location = New Point(5, 35)

            Me.Controls.Add(gbTB)

            gbTB.Size = New Size(Form1.TabControl1.Width - 10, 80)

            gbTB.Location = New Point(5, Form1.TabControl1.Height - gbTB.Height - 20)

            tbAddressbar.Width = gbTB.Width - 10

            tbAddressbar.Location = New Point(5, 20)

            Me.Controls.Add(gbwb)

            wb.Navigate("www.google.com")

        End Sub

     

     

     

        Private Sub tbAddressbar_KeyPress(ByVal sender As Object, _

                    ByVal e As System.Windows.Forms.KeyPressEventArgs) _

                    Handles tbAddressbar.KeyPress

            If e.KeyChar = Chr(Keys.Enter) Then

                wb.Navigate(tbAddressbar.Text)

                e.Handled = True

            End If

        End Sub

     

        Public Sub Dispose1()

            tbAddressbar.Dispose()

            gbTB.Dispose()

            wb.Dispose()

            gbwb.Dispose()

            Me.Dispose()

        End Sub

     

    End Class

     

     

     

     

     

     

    Friday, January 26, 2007 9:54 PM
  • You are absolutely right, Renee. I guess I should keep my mouth shut :)

    Encapsulating everything into a class or control is definately a better design: and if I was actually doing it, would be the way to go. I suppose it comes down to the difference between getting a task done (my way), and getting it done right (your way). The latter is actually quicker than the former. :)

    Friday, January 26, 2007 9:59 PM
  • Thanks a load Renee
    Friday, January 26, 2007 10:55 PM
  •  

    You are welcome. You have a lot of work to do !!!!!!!

    Friday, January 26, 2007 10:57 PM
  • I see someone who bought the book and code samples.

    It's simple use the tab interface and add 10 of them and people will be limited to their usual tab amount.

    Optionally you could generate tabs upto 10 and use a combobox navigation selector to navigate each tab! Easy! If not try to learn C++ that Internet Explorer 6/7 Runtime Library uses. I recommend SkinCrafter skins for them too!

    Monday, April 23, 2007 4:07 PM
  • Or a textbox built into the tab? (Requires C++ Handling).
    Monday, April 23, 2007 4:09 PM
  • If you use 10 of them (standard IE7 = 100). Them = Tab control.

     Then add a home page for each one of them on a settings page.

    Sample:

    SampleBrowser|Settings|TXTFile|

    TXTFile is make default registry (C++ Only).

    Form1 (SampleBrowser):

    Code Snippet

    1public class

    2      private sub form1_load(blahblahblah)

    3            browser1.gohome() '//Try to use a settings command.

    4            '//Properties of settings.textbox1.text>application settings>text as hometext1.

    5            tabcontrol1.items.add(browser1.url.tostring)

    6            '//Open 3 tabs loading the 3 web pages.

    7     '//BLAH BLAH!

    8     end sub

    9     '//BLAH, BLAH!

    10     private sub addtab_click('//BLAH, BLAH) handles addtab.click

    11            tabcontrol1.items.add(settings.textbox1.text)

    12     end sub

    13end class

     

    PS. If you also want to know how favourites menu works, it is a file list from folders to open as a html file (cache to the current default browser or instance!).

     

    Monday, April 23, 2007 4:26 PM
  • If you use 10 of them (standard IE7 = 100). Them = Tab control.

     Then add a home page for each one of them on a settings page.

    Sample:

    SampleBrowser|Settings|TXTFile|

    TXTFile is make default registry (C++ Only).

    Form1 (SampleBrowser):

    Code Snippet

    1public class

    2      private sub form1_load(blahblahblah)

    3            browser1.gohome() '//Try to use a settings command.

    4            '//Properties of settings.textbox1.text>application settings>text as hometext1.

    5            tabcontrol1.items.add(browser1.url.tostring)

    6            '//Open 3 tabs loading the 3 web pages.

    7     '//BLAH BLAH!

    8     end sub

    9     '//BLAH, BLAH!

    10     private sub addtab_click('//BLAH, BLAH) handles addtab.click

    11            tabcontrol1.items.add(settings.textbox1.text)

    12     end sub

    13end class

     

    PS. If you also want to know how favourites menu works, it is a file list from folders to open as a html file (cache to the current default browser or instance!).

    Thankyou 3900 Page C++, C#, C+, VB, VB.NET Manual!

     

    Monday, April 23, 2007 4:29 PM