Need help with tab errors!

fantity

Active member
Joined
Mar 10, 2012
Messages
27
Programming Experience
Beginner
I am currently making a tabbed browser. I can open as many tabs as I want and close how many I want. But when I have closed all of them, and I am at my first tab, when I try to open a new one from my first tab the browser crashes and gives me this error:
VB.NET:
InvalidArgument=Value of '#number of tabs that were open' is not valid for 'index'.
This is my code for adding a new tab:
VB.NET:
Private Sub NewTabToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewTabToolStripMenuItem.Click        
        Dim Browser As New WebBrowser
        TabControl1.TabPages.Add("New Page")
        TabControl1.SelectTab(int)
        Browser.Name = "Web Browser"
        Browser.Dock = DockStyle.Fill
        TabControl1.SelectedTab.Controls.Add(Browser)
        AddHandler Browser.DocumentCompleted, AddressOf Done
        int = int + 1
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("xxxxx")
    End Sub

And this is the code for the index: Dim int As Integer = 0

Please help me!
 
That's no surprise. You should never be using the number of items in a collection as an index into that collection because the upper bound is always one less than the number of items.
 
Why do you even need that 'int' variable (which is a terrible name by the way)? If you want to know how many TabPages are in the the TabControl then, just like any collection, you just get the Count of the TabPages property. If you want to select the TabPage you just added then you don't even need to know what index it is. Add returns the TabPage itself so you can simply assign that to the SelectedTab property. If you want to select the last tab then, as I said, its index will be one less than the Count, so you can assign that to the SelectedIndex.
 
Add returns the TabPage itself so you can simply assign that to the SelectedTab property.
Add method doesn't return anything. Many collection methods return the object created from simple values, but TabPageCollection does not. The index of added tabpage is (.TabCount-1). Example:
        Dim browser As New WebBrowser
        With browser
            .Name = "Web Browser"
            .Dock = DockStyle.Fill
            AddHandler .DocumentCompleted, AddressOf Done
        End With
        With TabControl1
            .TabPages.Add("New Page")
            .SelectTab(.TabCount - 1)
            .SelectedTab.Controls.Add(browser)
        End With    
        browser.Navigate("xxxxx")
 
Add method doesn't return anything. Many collection methods return the object created from simple values, but TabPageCollection does not. The index of added tabpage is (.TabCount-1). Example:
        Dim browser As New WebBrowser
        With browser
            .Name = "Web Browser"
            .Dock = DockStyle.Fill
            AddHandler .DocumentCompleted, AddressOf Done
        End With
        With TabControl1
            .TabPages.Add("New Page")
            .SelectTab(.TabCount - 1)
            .SelectedTab.Controls.Add(browser)
        End With    
        browser.Navigate("xxxxx")

I used your code and it was working fine until I made a 4th tab which I got this error:
Capture.PNG
 
In DocumentCompleted event handler you have to check if browser.ReadyState is Complete first.
Based on browser not being Nothing the error obviously means Url is Nothing at that time.
 
Sorry about the misinformation. I'm on a phone and wasn't really able to look it up so quoted that from memory. I think that I was thinking of a TreeView rather than a TabControl. Hey, they both start with T! :blush:
 
check if browser.ReadyState is Complete first.
If browser.ReadyState=WebBrowserReadyState.Complete Then
Don't you hate hints? :anonymous:

Another thing, you have a single event handler for multiple browsers, can you be sure it is the browser on the currently selected tab that raises the event? I would say no, and that you should use the sender event parameter to identify which browser to work with. The event 'sender' object is always the class instance that raised the event.
 
If browser.ReadyState=WebBrowserReadyState.Complete Then
Don't you hate hints? :anonymous:

Another thing, you have a single event handler for multiple browsers, can you be sure it is the browser on the currently selected tab that raises the event? I would say no, and that you should use the sender event parameter to identify which browser to work with. The event 'sender' object is always the class instance that raised the event.

I did it and the tabs open and close just fine but the url doesn't show up in the combobox anymore. Here is the code:

VB.NET:
Private Sub Done(ByVal sender As Object, ByVal e As Windows.Forms.WebBrowserDocumentCompletedEventArgs)
        Dim browser As New WebBrowser
        With browser
        End With
        TabControl1.SelectedTab.Text = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).DocumentTitle
        If browser.ReadyState = WebBrowserReadyState.Complete Then
            ComboBox2.Text = CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Url.ToString
        End If
    End Sub
 
this is the relevant browser instance for event handler:
Dim browser = CType(sender, WebBrowser)

this is the tabpage for that browser instance:
Dim page = CType(browser.Parent, TabPage)

You should not access anything else about the browser before document is completely loaded.
 
this is the relevant browser instance for event handler:
Dim browser = CType(sender, WebBrowser)

this is the tabpage for that browser instance:
Dim page = CType(browser.Parent, TabPage)

You should not access anything else about the browser before document is completely loaded.

Now tabs won't open and there is no url :S
Capture.PNG
 
this is the relevant browser instance for event handler:
Dim browser = CType(sender, WebBrowser)

this is the tabpage for that browser instance:
Dim page = CType(browser.Parent, TabPage)

You should not access anything else about the browser before document is completely loaded.


Nvm. I put it in the wrong spot :p Thanks
 
Back
Top