VB.Net: Tabcontrol and webbrowser control


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 3 123 LastLast
Results 1 to 15 of 33

Thread: VB.Net: Tabcontrol and webbrowser control

  1. #1
    Join Date
    Oct 2004
    Posts
    47

    VB.Net: Tabcontrol and webbrowser control

    I've got this little wannabe-webbrowser project that uses the Webbrowser control inside a Tabcontrol.
    For opening a new tab+browser I've got this:

    Dim n As Integer
    Dim w as New WebBrowser
    n = tabcontrol1.TabPages.Count + 1
    Dim t As New TabPage
    t.Text = "Page " & n
    w.Dock = DockStyle.Fill
    w.Navigate("about:blank")
    t.Controls.Add(w)
    Me.TabControl1.Controls.Add(t)

    Now once I've got it up, how can I tell the Tabcontrol1.Selectedtab's webbrowser control to navigate to cboURL.text?
    So, with other words, what should be in place of x in the following lines of code:

    cboURL.Items.Add(cboUrl.Text)
    x.Navigate(cboUrl.Text)

    ?

    Thnx in advance, graey.
    geert

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    w.

    cboURL.Items.Add(cboUrl.Text)
    w.Navigate(cboUrl.Text)
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Oct 2004
    Posts
    47
    im sorry if I was unclear.
    every webbrowser is called w.
    So on tab 1 a browser is called w, on tab 2 a browser is called w, and so on...
    and also these arent public declarations, so W is only available within the form's load event...
    geert

  4. #4
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    I would probably create a form-level collection (e.g., ArrayList) to contain the WebBrowser controls. Then you could do this:

    Dim w As New WebBrowser
    BrowserCollection.Add(w)
    .
    .
    .
    Dim ActiveBrowser As WebBrowser = CType(BrowserCollection(TabControl1.TabIndex), WebBrowser)
    ActiveBrowser.Navigate(cboUrl.Text)
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  5. #5
    Join Date
    Oct 2004
    Posts
    47
    hmm im using vb.net 2005, dunno if that should give difficulties, but i get an error when adding a browser to the arraylist:

    [title]NullReferenceException was unhandled.[/title]
    Object reference not set to an instance of an object.

    as Im new to .net, I don't know what to do with this...
    geert

  6. #6
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Please post your code.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  7. #7
    Join Date
    Oct 2004
    Posts
    47
    Public Class Main
    Public i As Integer
    Dim Browsercollection As ArrayList




    Private Sub newtabToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newtabToolStripMenuItem.Click
    Dim w As New WebBrowser
    Dim t As New TabPage
    t.Controls.Add(w)
    Browsercollection.Add(w)
    cboURL.Width = (Me.Width / 2)
    w.Dock = DockStyle.Fill
    w.Navigate("about:blank")
    t.Text = "Tab " & i
    tabControl1.Controls.Add(t)
    i = i + 1
    End Sub

    Private Sub cmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGo.Click
    cboURL.Items.Add(cboURL.Text)
    Dim ActiveBrowser As WebBrowser = CType(Browsercollection(tabControl1.TabIndex), WebBrowser)
    ActiveBrowser.Navigate(cboURL.Text)
    End Sub

    End Class

    Obviously there were some more items in it, but these are not important (like wether the statusbar is visible and stuff).
    geert

  8. #8
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    You're declaring a variable of type ArrayList, but you're never creating an instance of it (New).

    Change:
    Dim BrowserCollection As ArrayList

    To:
    Dim BrowserCollection As New ArrayList

    Generally, when you get a NullReferenceException it means you've forgotten a New.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  9. #9
    Join Date
    Oct 2004
    Posts
    47
    thnx
    however this error pops up now:

    ArgumentOutOfRangeException was unhandled.
    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    Code:

    Code:
        Private Sub cmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGo.Click
            cboURL.Items.Add(cboURL.Text)
            (this line gave the error)Dim ActiveBrowser As WebBrowser = CType(Browsercollection(tabControl1.TabCount), WebBrowser)
            ActiveBrowser.Navigate(cboURL.Text)
        End Sub
    EDIT: i changed tabcontrol1.tabindex into tabcount, still the same error tho...
    Last edited by geertvdijk; 03-01-2006 at 08:10 PM.
    geert

  10. #10
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    OK, you should be able to figure that out: What is the value of tabControl1.TabIndex when the error occurs? How may items are in the BrowserCollection arraylist? Do the math. ;-)
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  11. #11
    Join Date
    Oct 2004
    Posts
    47
    the strange thing is, no matter how many tabs are open, tabindex is always 6...
    and logicly tabcount doesnt work cause it doesnt show the active tabs nr....
    geert

  12. #12
    Join Date
    Oct 2004
    Posts
    47
    i see, in vb.net 2005 it is selectedindex...
    dunno how it is in earlier .net versions btw... xD
    geert

  13. #13
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Sorry, my mistake. TabIndex is the control's position in the tab order.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  14. #14
    Join Date
    Oct 2004
    Posts
    47
    ofcourse i could have know that xD
    thanks for all of your help, tho...
    is there btw any way of calling the webbrowser's find/replace form? xD
    geert

  15. #15
    Join Date
    Jul 2007
    Posts
    5

    Question

    Hello,

    I am trying to do the same make a simple browser with tabs and when I tried out what is above it didn't work for me?

    I get this error:

    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    Would anyone know why?

    My Code is like this:

    Dim w As New WebBrowser
    BrowserCollection.Add(w)
    Dim ActiveBrowser As WebBrowser = CType(BrowserCollection(Me.TabControl1.SelectedIndex), WebBrowser)
    ActiveBrowser.Navigate(Me.ComboBox1.Text)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center