views:

346

answers:

3

I've got a web browser control that needs to have a specific height set. If the user has a small amount of text, I want the web browser to be short. If the user types alot of text, then I need the web browser to be alot taller.

How can I find the height of the rendered text from the WebBrowser?

I saw this Getting the page height from a C# web browser control but thought there was a better way.


Update

I'm setting the text of the browser with:

webBrowser1.DocumentText = value;

After I set this value, I can check the webBrowser1.Document.Body and its null. I can also get all children of the document and none are returned.

Am I setting the correct property?

+1  A: 

As the answer for that question:

Find the BODY tag and get the OffsetRectangle.Bottom of that element. This will give you the height of the page.

I don't see the need for any better way, it seems rather easy to me.


Does webBrowser1.DocumentText return anything? Your site contains the body tag, right?

Phoexo
Then I must be doing something else wrong... Let me update my question
Miles
Ya, i'm setting the DocumentText = "<html><body>some text</body></html>". after its set, the DocumentText is just "<html></html>". its like it doesn't set it
Miles
Found the answer. I was checking for this value write after I set it. I looked at the event of when the document was done loading and checked it there. BOOM. there is was. Thanks
Miles
+2  A: 

Something I've noticed with webBrowser.DocumentText is that you cannot necessarily "get" it's value immediately after setting it it.

webBrowser1.DocumentText = "Some Text";

labelHtml.Text = webBrowser1.DocumentText

This will not produce predictable results because the webbrowser control may not have finished rendering the text yet. If it is run repeatedly, and "Some Text" is a variable, you will see that "labelHtml" quickly becomes out of sync with what is in your browser control. (The more complex "Some Text" is the longer it takes to render naturally.)

If you use the WebBrowser control's webBrowser1_documentCompleted event handler to obtain your DocumentText, you should be fine.

    private void setText(string htmlText)
    {
            webBrowser1.DocumentText = htmlText;
    }


    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        label1.Text = webBrowser1.DocumentText;
    }

label1 will now contain what you provide the web browser with...

Stuart Helwig
Oops, posted after your comment on Phoexo's answer...wasted my time then ??
Stuart Helwig
A: 

I see a reason to do it another way...

The referenced article "Getting the page height from a C# web browser control" utilizes System.Windows.Forms.HtmlElementcollection which does not exist in the compact framework API. I am searching for solution that works within .NET CF also. I would be interested in another solution for that reason.

Ben Horgen