VB.NET - Scroll to bottom of WebBrowser Control af

2019-09-08 00:27发布

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

标签: vb.net
2条回答
做自己的国王
2楼-- · 2019-09-08 00:41
wb1.Navigate("javascript:window.scroll(0,document.body.scrollHeight);")
查看更多
家丑人穷心不美
3楼-- · 2019-09-08 00:48

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
查看更多
登录 后发表回答