Reputation: 5976
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
Reputation: 1
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
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
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
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
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