views:

420

answers:

1

I'm trying to load up a YouTube page using Visual Studio 2008 and a very simple Visual Basic project that contains just a WebBrowser. I want to load the page when the form loads:

Private Sub Form1_Load(ByVal sender As Object, 
    ByVal e As System.EventArgs) Handles Me.Load
    WebBrowser1.Navigate("http://code.google.com/apis/youtube/js_example_1.html")
End Sub

Every now & then (occurs once every 3-5 times, but varies), the form just shows a blank white box.

Could someone help me understand why this happens please.

Edit: This 'freeze' happens for any page, such as http://www.google.com. Is my WebBrowser control broken? Only seems to happen when i place the navigate code in the Form Load event, hmmm strange.

A: 

This is probably (almost certainly) happening because a form's Load event occurs before its first Paint event, so once in awhile the WebBrowser finishes navigating to Google (or wherever) before the form paints itself the first time, and thus the WebBrowser shows up as just a white box.

A very simple solution to this problem is to just put a Timer control on the form with a short Interval (say, 100 ms), set Enabled to true, and in its Tick event disable the Timer and then call Navigate(...) on your WebBrowser.

MusiGenesis
Thanks for the reply MusiGenesis.I tried the timer trick, but still the same. I then tried to WebBrowser1.Navigate in the Form Show event, which is only called after the form loads...also freezes occasionally.I then removed all code and added a button. In the button click event i added the WebBrowser1.Navigate and it also freezes every now and then.It's frustrating because it seems sporadic, so i can't pin-point the error.
net noobie
WebBrowser can be a weird, spastic control sometimes, because it's really just showing an instance of whatever version of Internet Explorer you happen to have installed on your machine. I assume you don't ever have problems like this when you navigate to these web sites with an ordinary browser instance?
MusiGenesis
Yep, no problems in a 'mormal' browser.I also get javascript permission problems on 64-bit vista machines. I think your summary hits the nqail on the head..."WebBrowser can be a weird, spastic control sometimes" :)Just wish i learnt Flash instead of .NET.
net noobie
@net noobie: maybe switch to C#? I just created a little sample app with a WebBrowser on a form that navigates to your sample URL in the form's Load event, and it loaded up with no problems about 50 straight times. I'm on Windows Vista, with IE 7.0.6000.16890. One more thing you could try is to check the Event log right after a failure, and see if IE wrote anything useful in there.
MusiGenesis
Thanks MusiGenesis. I have a lot of code hanging off the VB webbrowser, so i guess it would need to be refactored. Could just create a webbrowser dll and import that assembly. It's just strange that it works 80-90% of time and even more strange that it fails more often if i have left FireFox open for a while and it consumes about 500MB of RAM. I think the solution would be ASP, C# or Flash. I will try the event log thingy; thanks for that suggestion. Never used it before.
net noobie