4

I have the following code that, weirdly, works for a couple of seconds and then stops working (my event handler method stops being called):

public partial class Form1 : Form
{
    private void Form1_Load(object sender, EventArgs e)
    {
        webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
        webBrowser1.Navigate("google.com");
    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if (!webBrowser1.IsBusy && webBrowser1.Url == e.Url && webBrowser1.ReadyState == WebBrowserReadyState.Complete)
        {
            HTMLWindowEvents_Event windowEvents = webBrowser1.Document.Window.DomWindow as HTMLWindowEvents_Event;
            windowEvents.onscroll += new HTMLWindowEvents_onscrollEventHandler(windowEvents_onscroll);
        }
    }

    private void windowEvents_onscroll()
    {
        HtmlDocument htmlDoc = webBrowser1.Document;
        int scrollTop = htmlDoc.GetElementsByTagName("HTML")[0].ScrollTop;
        string text = scrollTop.ToString();
    }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Juan
  • 15,274
  • 23
  • 105
  • 187
  • Either post all of the code or give more info. This really isn't enough to debug the code (unless someone was trying to do the EXACT same thing as you are). – Stefan Mai Dec 03 '10 at 07:59
  • There is no other code than assigning the `DocumentCompleted` event of my `WebBrowser` to my `OnDocumentCompleted` method and navigating to google on the `Load` event of my `Form`. That's all. – Juan Dec 03 '10 at 08:01
  • But anyway, I'll post the whole thing if that will help you help me. – Juan Dec 03 '10 at 08:06
  • Cannot reproduce. It works fine here... – Stefan Mai Dec 03 '10 at 08:36
  • Did it work for more than a few seconds? You have to keep scrolling up and down for about 5 - 10 secs. – Juan Dec 03 '10 at 08:37
  • @JuanLuisSoldi What was you assembly reference for HTMLWindowEvents_Event ? – Pomster Jun 20 '12 at 08:06

1 Answers1

7

OK Found a solution:

    protected override void OnDocumentCompleted(WebBrowserDocumentCompletedEventArgs e)
    {
        Follow();
        if (!IsBusy && Url == e.Url && ReadyState == WebBrowserReadyState.Complete)
        {
            Document.Window.AttachEventHandler("onscroll", DocScroll);
        }
    }

If attached that way it works OK (so far...). Don't even need to use mshtml.

Juan
  • 15,274
  • 23
  • 105
  • 187