Question

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?

Was it helpful?

Solution

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);

OTHER TIPS

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);

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 :)

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
 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);
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top