eqiz
eqiz

Reputation: 1591

VB.NET - Scroll to bottom of WebBrowser Control after DocumentText Update

So i have looked at several other posts on stackoverflow and none of them seem to be working for me to accomplish this. All I want to do is for the WebBrowser control to automatically scroll down to the very bottom after I change something programmatically in the webbrowser1.documenttext property.

I've tried ALL of the following ways, and none of them work... I actually have them literally all in the exact same line of code.

     WebBrowser1.ScrollBarsEnabled = True
    WebBrowser1.Document.Body.ScrollIntoView(False)

    WebBrowser1.Document.Window.ScrollTo(New Point(WebBrowser1.Height, WebBrowser1.Height))



    WebBrowser1.Document.Window.ScrollTo(WebBrowser1.Height, WebBrowser1.Height)
    WebBrowser1.AutoScrollOffset = New Point(WebBrowser1.Height, WebBrowser1.Height)

In my WebBrowser1 control all I have done is set started it with and then at the end placed and in the middle all i've done is copied 'n pasted the following...

    <html><body>
   <div style="color: red;">blah blah</div><Br />
   <div style="color: red;">blah blah</div><Br />
   <div style="color: red;">blah blah</div><Br />
   <div style="color: red;">blah blah</div><Br />
   <div style="color: red;">blah blah</div><Br />
   <div style="color: red;">blah blah</div><Br />
   <div style="color: red;">blah blah</div><Br />
   <div style="color: red;">blah blah</div><Br />
   <div style="color: red;">blah blah</div><Br />
   <div style="color: red;">blah blah</div><Br />
   <div style="color: red;">blah blah</div><Br />
   <div style="color: red;">blah blah</div><Br />
   </body></html>

about 30 times... BUT I can't get anything to work. None of the code above is doing anything.

What am I missing?

VS 2005 SP1 - VB.NET

Upvotes: 0

Views: 9167

Answers (2)

anonymous
anonymous

Reputation: 21

wb1.Navigate("javascript:window.scroll(0,document.body.scrollHeight);")

Upvotes: 1

John Koerner
John Koerner

Reputation: 38087

Your problem may be you are trying to access the DOM before it is updated. Fire your code to scroll the correct element into view in the DocumentCompleted event, like this:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    WebBrowser1.DocumentText = <html><body>
                                   <div style='color: red;'>blah blah</div><Br/>
                                   <div style='color: red;'>blah blah</div><Br/>
                                   <div style='color: red;'>blah blah</div><Br/>
                                   <div style='color: red;'>blah blah</div><Br/>
                                   <div style='color: red;'>blah blah</div><Br/>
                                   <div style='color: red;'>blah blah</div><Br/>
                                   <div style='color: red;'>blah blah</div><Br/>
                                   <div style='color: red;'>blah blah</div><Br/>
                                   <div style='color: red;'>blah blah</div><Br/>
                                   <div style='color: red;'>blah blah</div><Br/>
                                   <div style='color: red;'>blah blah</div><Br/>
                                   <div style='color: red;' id="lastElement">blah blah</div><Br/>
                                   </body></html>.ToString()
End Sub

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    Dim lastGuy= WebBrowser1.Document.GetElementById("lastElement")
    If lastGuy<> Nothing Then
        lastGuy.ScrollIntoView(True)
    End If
End Sub

Upvotes: 1

Related Questions