WebBrowser control not working?

drakeseebo
06-29-2007, 03:38 AM
My WebBrowser control does not seem to be working fully.

I can navigate to pages and do simple stuff like that, but Document.links isn't working. The only thing that is even coming up under .Document is .GetType

Heres my code.

Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Dim i As Object
AxWebBrowser1.Navigate2("www.google.com")
If AxWebBrowser1.Document.links > 0 Then
For i = 0 To AxWebBrowser1.Document.Links.length - 1
If AxWebBrowser1.Document.links(i).href = "http://www.google.com/intl/en/about.html" Then
AxWebBrowser1.Navigate2(AxWebBrowser1.Document.links(i).href)
End If
Next
End If
End Sub

I'm getting this error on "If AxWebBrowser1.Document.links > 0 Then" when trying to run the program.
An unhandled exception of type 'System.NullReferenceException' occurred in microsoft.visualbasic.dll

Additional information: Object variable or With block variable not set.

BTW I'm using Visual Basic .NET 2003

Any ideas? Thanks :D

AtmaWeapon
06-29-2007, 09:19 AM
Have you used the debugger to see which object is empty? It could be Document or links. It's also possible the document isn't fully loaded if Navigate2 is an asynchronous call. If that's the case, you need to wait until you get whatever event fires when the control finishes loading the page; my guess is NavigateComplete2.

I made a test project and moved everything after "AxWebBrowser1.Navigate2("www.google.com")" into an event handler for Navigate2 and it worked. Be warned that as far as I can tell the Links collection is empty.

drakeseebo
06-29-2007, 06:57 PM
Well with "Option Strict On" all of the "WebBrowser1.Document.Links"'s in my code are saying "Option Strict On disallows late binding".

I tried what you said about making an event NavigateComplete2. The program now runs without errors but the event is not firing. Also with "Option Strict On" I am now getting the message "Option Strict On disallows late binding" for only "WebBrowser1.Document" not .links with it.

With "Option Strict Off" the program runs, goes to google and just sits there.

Heres the code, I think I'm not making the event right...

Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Dim URL As Object
URL = "http://www.google.com/"
WebBrowser1.Navigate2(URL)
End Sub

Private Sub WebBrowser1_NavigateComplete2(ByVal WebBrowser1 As Object, ByVal URL As Object)
Dim i As Object
If WebBrowser1.Document.Links > 0 Then
For i = 0 To WebBrowser1.Document.Links.length - 1
If WebBrowser1.Document.links(i).href = "http://www.google.com/intl/en/about.html" Then
WebBrowser1.Navigate2(WebBrowser1.Document.links(i).href)
End If
Next
End If
Me.Close()
End Sub


Edit: Also is there any other ways I can wait for the page to load? Without making an event? I'm still quite new to VB and its been awhile since I used it so I am very rusty at it.

Edit: Wow I just sort of got it working, not how I want it to, but now I at least know my event is coded wrong...

I made a button on the form and when I hit the button it does the code that was in the event. That works fine, so that must mean I'm coding the NavigateComplete2 event wrong, but i have no idea whats wrong with it? Any ideas?

Edit: Ok I fixed it, Its working fine. I totally forgot about the Class Name and Method Name in VB :/ That how long it has been since I've used VB.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum