Here is the error i get
Object reference not set to an instance of an object
and also
index 0 is out of range parameter name: index

Public Class Form1
    Dim int As Integer = 0

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoBack()
    End Sub

    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoForward()
    End Sub

    Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Stop()
    End Sub

    Private Sub PictureBox4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox4.Click
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Refresh()
    End Sub

    Private Sub PictureBox5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox5.Click
        Dim Browser As New WebBrowser
        TabControl1.TabPages.Add("New Page")
        TabControl1.SelectTab(0)
        Browser.Name = "Web Browser"
        Browser.Dock = DockStyle.Fill
        TabControl1.SelectedTab.Controls.Add(Browser)
  
        int = int + 1
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoHome()

    End Sub

    Private Sub PictureBox6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox6.Click
        If Not TabControl1.TabPages.Count = 1 Then
            TabControl1.TabPages.RemoveAt(TabControl1.SelectedIndex)
            TabControl1.SelectTab(TabControl1.TabPages.Count - 1)
            int = int - 1
        End If
    End Sub

    Private Sub PictureBox7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox7.Click
        CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(ComboBox1.Text)
    End Sub
End Class

If you can help it would be greatly appreciated i am using Visual Basic 2008.

Recommended Answers

All 20 Replies

"index 0 is out of range"
This error means that there is nothing in the particular collection at position 0.

I.e. The collection is empty.

At what line in the code are the errors thrown?

ummm... i dont know to be honest ot only happens when i clikc on the picture boxes

Ok, I recommend putting Try Catch blocks around your code and placing a break in the catch portion so the program will break when you have an error and you can find out where it's happening.

I have done it for you:

Public Class Form1
    Dim int As Integer = 0

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        Try
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoBack()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
        Try
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoForward()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
        Try
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Stop()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub PictureBox4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox4.Click
        Try
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Refresh()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub PictureBox5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox5.Click
        Try
            Dim Browser As New WebBrowser
            TabControl1.TabPages.Add("New Page")
            TabControl1.SelectTab(0)
            Browser.Name = "Web Browser"
            Browser.Dock = DockStyle.Fill
            TabControl1.SelectedTab.Controls.Add(Browser)

            int = int + 1
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoHome()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub PictureBox6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox6.Click
        Try
            If Not TabControl1.TabPages.Count = 1 Then
                TabControl1.TabPages.RemoveAt(TabControl1.SelectedIndex)
                TabControl1.SelectTab(TabControl1.TabPages.Count - 1)
                int = int - 1
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub PictureBox7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox7.Click
        Try
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(ComboBox1.Text)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class

ok so this is what i got

System.nullreferanceEception:Object referance not set to an instance of an object.
at windowsApplication1.form1.pivturebox7_Click(Objectsebder.eventArgs e) in C:\Documents and settings\my name\my documents\visualBasic\my tabedWebBrowser\webBrowser.vb

this happends in lines 30, 55 and 66

Could be a couple of things:
1) the "TabControl1.SelectedTab" property is nothing. Either click a tabpage or select it through code. e.g. TabControl1.SelectedTab = TabControl1.TabPages(0) (not sure of the syntax, you get the idea)

2) Your WebBrowser control doesn't exists in the tabpage collection of controls at item 0.

It could be something else but that should get you going. I would put a code break on lines 30, 55 and 66 and have a look at the code to see just where the problem is happening.

ok i will try that

i did that and i still get the same errors?
And sorry took so long to get back to you been busy.

Did you put the code breaks in and have a look at the values of the TabControl?

Look to see if the "SelectedTab" is nothing, if its nothing then you have not selected a Tabpage or if its not nothing then look at the count property of the SelectedTab's controls collection (e.g. TabControl1.SelectedTab.Controls.Items.count) Check to see if the count is zero.

If its zero then there are no controls in the SelectedTab.

To check these values, place the code breaks as mentioned earlier and while the compiler has stopped at your code break you need to highlight the property you are interested in and hover your mouse over it. You should get a pop up control that will let you look at the properties of the object you are hovering.

Yes the tab control is set to zero. And i did do the Code Breaks above.
And ive had the tab control set to zero and 1 and i have added a tab page to see what that would do and i still get the errors as mention above.

<quote>Yes the tab control is set to zero.</quote>

I'm assuming you mean the Tabpage.Controls.Items.count = 0, is this correct? Sorry emails/user group messages can be difficult...

That would mean that your WebBrowser control has not been loaded into the tabpage you are trying to access. Do you have more than one tab page? If yes, is the WebBrowser control in all of those pages?

<quote>i have added a tab page to see what that would do</quote>
If the WebBrowser was not in the tabpage you added then you would get the same results.

Can you post the code where you are trying to add in the tabpage?

How big is the project? Can you post it?

i can give you a download link for the project if you would like here it is yes i have virus scanned it and there isn't any http://www.mediafire.com/?nqwbkddt6sjms6j.
as you can see i am not very good as i have just started teaching MYSELF how to program 2 months ago.

I really meant the code itself and not the exe. This way I can have a look at the code while its running.

When I run the exe I noticed that there are no tabpages in the tab control, it's blank. You will need to add a tabpage and then add your WebBrowser control in the tabpage.

From Visual Studio click on the Tab Control then find the Items property and from there add a tabpage. Then drop in your WebBrowser control.

Ok. here is the code itself. with added tab in VB form

Public Class Form1
    Dim int As Integer = 0

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        Try
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoBack()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
        Try
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoForward()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
        Try
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Stop()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub PictureBox4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox4.Click
        Try
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Refresh()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub PictureBox5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox5.Click
        Try
            Dim Browser As New WebBrowser
            TabControl1.TabPages.Add("New Page")
            TabControl1.SelectTab(0)
            Browser.Name = "Web Browser"
            Browser.Dock = DockStyle.Fill
            TabControl1.SelectedTab.Controls.Add(Browser)

            int = int + 1
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).GoHome()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub PictureBox6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox6.Click
        Try
            If Not TabControl1.TabPages.Count = 1 Then
                TabControl1.TabPages.RemoveAt(TabControl1.SelectedIndex)
                TabControl1.SelectTab(TabControl1.TabPages.Count - 1)
                int = int - 1
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub PictureBox7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox7.Click
        Try
            CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(ComboBox1.Text)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
  
End Class

Thats is the code with the added tab page in the design form.

The code works perfect for me. Here is what I did:
1) Created a form
2) Copied/Pasted your code
3) Dropped in a TabControl AND removed all tabpages
4) Create 7 pictureboxes along the top of me form
5) Created the textbox

What I did was to click on PictureBox5 FIRST. This created the tabpage and the web browser first.

Then after that I was able to click on any other picture box without receiving any errors.

Make sure you click on PictureBox5 first and see how you get on.

Yes that does work but do you happen to know how i would go about fixing it to where i don't have to hit picture box 5 first and i can just use it like i would any other web browser?
And thank you.

Also out of curiosity did you try and go to google because when i do i get like 100 errors i think it is google and not my browser but just wondering if you could maybe give me an idea on what could be wrong?

The issue or errors you are getting is because you haven't loaded a tabpage containing your WebBrowser control into the main TabControl.

Before you try to access the control you can check to see if the control has been added and if not add one in.

Another way and maybe easier is in the Form_Load event you can load the tabpage and WebBrowser control and know that it is done.

HTH

OK i will do that and do you know why it doesnt work with google or is it just google?

Google.com works for me. I can go anywhere using this form. It must be something on your end.

Ok but thank you for your help it help it was greatly appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.