views:

31

answers:

1

I'm using an HTML document to show text output in my application - this gives me the flexibility of HTML and the power of CSS, and I can publish the generated HTML as a log file openable in any browser. When new information is available, I update the HTML file and then call Refresh() on my WebBrowser control. Naturally, I want the user to see the newest information without having to manually scroll down, so I need to automatically scroll to the bottom of the document after the browser refreshes and changes have been rendered.

I have the code to scroll to the bottom:

(this.browser.Document as HTMLDocument).body.scrollIntoView(false);

The issue is that I've tried many events, and all of them fire BEFORE the web browser control has actually finished rendering the updated source, so I end up scrolling to the bottom before the new content is displayed! This is incredibly frustrating - does anyone know how to solve this problem? How can I know when the new content is COMPLETELY finished displaying so that I can scroll to the bottom of it?

EDIT: I tried enclosing a Frame inside a ScrollViewer:

<ScrollViewer x:Name="scrollBar" Grid.Row="1" Grid.Column="0">
    <Frame Name="browserBox" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" NavigationUIVisibility="Hidden"/>
</ScrollViewer>

This works fine for application pages (Page class) because the Frame grows large enough to fit the entire page, causing the enclosing ScrollViewer to adjust and fire a ScrollChanged event. I was momentarily very excited, but then discovered that unfortunately, when I load an HTML file into the frame instead, the frame does NOT resize to fit the content, but rather introduces its own scroll bar, so now I've got one scroll bar inside of another, and I can't subscribe to events for the inner scroll bar. :(

A: 

OMG! I found the answer! Based on a hint I found in this post:

Wrap a frame with a scroll viewer and subscribe to the frame's ContentRendered event:

<ScrollViewer x:Name="scrollViewer">
    <Frame x:Name="frame" Source="pack://siteoforigin:,,,/HTMLPage1.htm" NavigationUIVisibility="Hidden" ContentRendered="Frame_ContentRendered" />
</ScrollViewer>

On render, measure the HTML content and update the size of the frame (add a reference to .NET assembly Microsoft.mshtml):

using mshtml;

private void Frame_ContentRendered(object sender, EventArgs e)
{
    HTMLDocument htmlDoc = ((this.frame.Content as WebBrowser).Document as mshtml.HTMLDocument);

    if (htmlDoc != null && htmlDoc.body != null)
    {
        mshtml.IHTMLElement2 body = (mshtml.IHTMLElement2)htmlDoc.body; 
        this.frame.Width = body.scrollWidth; 
        this.frame.Height = body.scrollHeight;
    }

    this.scrollViewer.ScrollToEnd();
}

Finally, eliminate the scroll bar housed within the Frame by disabling scrolling within your HTML document itself:

<body scroll="no">

Woohoo! I'll go throw a party now. Scrolling to the bottom of an HTML doc should not have taken a week (but it did).

BigScary