Tim Laughlin's Everything VB.NET Blog


Webbrowser Control with Frames and multiple DocumentCompleted event firing

Back when I started this blog I had all the good intentions of keeping up with it.  Adding post daily.  Then of course life got in the way and I haven't posted in a while. 

However, today I was doing some work with the webbrowser control in VB.NET, for a new project.  Very cool control and really does a nice job exposing all of the IE object model.  But my project required the accurate processing of Frames.  I know frames, why!  The client uses them, not my idea.  I digress.  So when a framset is load the DocumentCompleted event is firing for each document that makes of the entire page, or window, for consistency of the object model.

Since I really was depending of DocumentComplete to fire once so I could do my processing I really needed the solution.  I asked Google, as I always do, and found a little Gem.

  Private Sub WebBrowser1_DocumentCompleted( _
    ByVal sender As Object, _
    ByVal e As WebBrowserDocumentCompletedEventArgs _
) Handles WebBrowser1.DocumentCompleted
    If Me.WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        MsgBox("Document and frames loaded!")
    End If
End Sub

the key being the ReadyState Property.  So simple! 

I found this here http://www.developersdex.com/vb/message.asp?p=1121&r=5125510 the answer was posted by Herfried K. Wagne.

Thanks for the answer, and thanks for the motivation to actually write this post.

If you are working with the webbrowser control and have not found it yet check out Working with the Web Browser Control in Visual Studio 2005 - IE7Clone. A great how to resource with nice example application.