Ryan Lanciaux
Ryan Lanciaux

Reputation: 5976

WebBrowserControl Scroll to Bottom

I am working on a simple chat application using a System.Windows.Forms.WebBrowser Control to display the messages between the user and the recipient. How do I get the control to automatically scroll to the bottom every time I update the DocumentText of the control?

Upvotes: 2

Views: 9200

Answers (5)

 public virtual void ScrollMessageIntoView()
        {

            System.Windows.Forms.Application.DoEvents();
            if (browser == null || browser.IsDisposed)
                return;

            if (browser.Document == null)
            {
                browser.Document.Window.ScrollTo(0,
                browser.Document.Body.ScrollRectangle.Height);
            }
        }

Upvotes: 0

IlPADlI
IlPADlI

Reputation: 2033

You can keep scroll position on top, and insert new message on top.

that don't need scroll to bottom, its look like twitter :)

user2:
  new message   ← a new message is insert on top

user1:
  old message

Upvotes: 0

Oded
Oded

Reputation: 499392

I would use the AutoScrollOffset property and set it the the bottom left of the WebBrowser control, so something like:

webCtrl.AutoScrollOffset = new Point(0, webCtrl.Height);

Upvotes: 2

Ryan Lanciaux
Ryan Lanciaux

Reputation: 5976

Thanks guys -- I voted you both up but neither would work out for my situation. What I ended up doing was

webCtrl.Document.Window.ScrollTo(0, int.MaxValue);

Upvotes: 5

David Mohundro
David Mohundro

Reputation: 12422

This is probably overkill, but you could also invoke script on the WebBrowser control and then use the scroll properties of the body tag. Or the scrollTo method of the window.

To invoke script, the WebBrowser control has a Document property that represents the document object from the DOM. It has a method called InvokeScript that you can pass a string of JavaScript to be executed.

But... if the AutoScrollOffset property works... yeah, I'd just use that instead of getting into JavaScript :)

Upvotes: 2

Related Questions