views:

823

answers:

2

I'm trying to get scrollbars working with the web browser control. However, as I'll be using it to display a message for a custom messagebox, I don't want the scrollbar to appear even if its not needed - as it seems to do by default. To circumvent this I decided to disable scrollbars on the control and instead use scrollbars on another control like a Panel. This way they'll only appear when the contents of the browser page is too big to fit.

This hasn't worked out too well, though I've read quite a few posts, even on StackOverflow, where this seems to be a valid solution. One example is when I tried using the solution here:

http://stackoverflow.com/questions/1798153/scrolling-problem-with-a-webbrowser-control-contained-in-a-panel-control

It seems as though if Scrollbars are disabled for the web browser, it won't let the panel use scrollbars either. This seemed to be the case when testing in design mode. To overcome this I tried adding a picture box behind the web browser inside the panel; it worked when in design mode (resizing the picture box and web browser would cause the panel to enable its scrollbars), but didn't work during runtime (I added code to have the picture box change to the size of the web browser control - which itself is always resized to fit the size of the scrollable contents).

I also tried programmatically enabling and disabling the web browser's scrollbars based on if the ScrollableRectangle size was bigger than the size of the control. This theoretically would be fine, except it seems to clear all text within the control any time the ScrollbarsEnabled property is changed - and thus is changed back to having no scrollbars.

I'm doing this with the following code, called effectively whenever a key is pressed in the control:

        if (Output.Document.Body != null)
        {
            if (Output.Document.Body.ScrollRectangle.Size.Height > Output.Size.Height
                    || Output.Document.Body.ScrollRectangle.Size.Width > Output.Size.Width)
                Output.ScrollBarsEnabled = true;
            else
                Output.ScrollBarsEnabled = false;
        }
        else
            Output.ScrollBarsEnabled = false;

It's also important to note that I also need a solution for the HTML editor which will be used within the app, so ideally any solution would not rely on a page load event etc... as these do not seem to trigger when the web browser has design mode set to on (which is needed for it to work as an HTML editor). However, in this particular situation I can fall back on enabling the default scrollbars if there's no better solution.

EDIT: To be clear, I am not talking about any scrollbars within the HTML content - that is of no concern as the HTML is simply being used to allow for flexible formatting of text. I'm talking only about the scrollbar of the browser control itself.

Any help much appreciated. Thanks!

+1  A: 

That's too late. IDocHostUIHandler.GetHostInfo is called when the webbrowser is created, and the WebBrowser's implementation sets DOCHOSTUIFLAG_SCROLL_NO or the DOCHOSTUIFLAG_FLAT_SCROLLBAR flag based on the value of its ScrollBarsEnabled property.

I suggest you to set ScrollBarsEnabled to false before creating the Webbrowser control's window.

If you don't want the scrollbar of a particular element, such as body, div or textarea, to appear, you can set their styles to overflow='hidden' or use scrolling properties specific to the elements such as doc.Body.SetAttribute("scroll","no").

Sheng Jiang 蒋晟
It's not any elements of the page which is an issue, it will only be used to display formatted text (already tried a richtextbox, it's not ideal). The problem for me is that the scrollbar down the right side of the web browser control itself is shown with the property enabled /even if/ the content fits perfectly within its current size. I basically only want the browsers scrollbar to appear on the web browser control when the content/page is too large to fit inside the web browser control. When you say "too late", do you mean programmatically changing the browser's scrollbar property?
Geekman
yes, IDocHostUIHandler.GetHostInfo is called before the first navigation.
Sheng Jiang 蒋晟
I had kind of already reached the conclusion that programmatically setting this property was not going to work...but isn't there another way someone can think of to achieve my desired result?
Geekman
Is there no way around this limitation? After reworking my application, the last thing I now need to do is optionally add a scrollbar if the form cannot resize large enough to fit ScrollRectangle's dimensions. Thanks!
Geekman
+1  A: 

I ended up finding a solution to this some time ago now, but forgot to post here. Basically what I did was first enable the scrollbars by default so they will work, although always appear. Then I created a panel control and sized it over the top of the inactive scrollbar that appears on the right of the web browser control.

Next I changed the anchors of the scroll bar panel so that the top, bottom, and right sides would always snap into the size of the form, and hence the size of the browser control as all edges of it too are anchored.

Then I added some code that checked the ScrollRectangle size and compared it with that of the browser, if it turned out to be larger in height or width, I then made the cover panel not visible, but otherwise left it in place.

Here's the relevant code snippets:

            //If still bigger, set scrollbars:

            if ((Output.Document.Body.ScrollRectangle.Size.Height > Output.Size.Height) ||
                Output.Document.Body.ScrollRectangle.Size.Width > Output.Size.Width)
            {
                ScrollPanel.Visible = false;
                ScrollPanel.Enabled = false;
            }

Hope this helps someone else in future, took a few different methods before I found one that worked well enough.

I've decided to up Sheng's answer (when I get enough reputation) as, firstly he was the only person to response, and secondly, his information helped me when I was contemplating the use of some kind of invocation to try and enable the scrollbars during runtime. Though I found my solution simpler and just as effective.

Geekman
Useful work-around, worked for me. +1 for you and Sheng.
fre0n