-2

I have a WinForm inside which there is a simple WebBrowser control. I want the Form should auto-resize depending on the length of WebBrowser content. If the content is small, the Form should shrink and If the content is big, it should grow.

These are the steps I have taken so for:

  1. I have a simple Form inside in which there is a WebBrowser Control.
  2. Docked the WebBrowser to Form Control.
  3. Set the properties of Form as below:

    • AutoSize: True
    • AutoSizeMode: GrowAndShrink
    • FormBorderStyle: FixedDialog
    • MaximizeBox: False

But still didn't work!

By doing so, the content of the form is completely disappeared.

Is there any solution for that?

M. Fawad Surosh
  • 450
  • 7
  • 20
  • Down-voter: any specific reason for down voting? – M. Fawad Surosh Feb 07 '17 at 17:18
  • Looks like you just quit before the job was done. You have to resize the WebBrowser, the form will follow. What *exactly* is it that you don't know how to do? Don't say "everything". – Hans Passant Feb 07 '17 at 17:22
  • You may find this post useful: [Size WPF Browser to its HTML-Content](http://stackoverflow.com/a/40795036/3110834). It can be applied on a Windows Form application. – Reza Aghaei Feb 07 '17 at 17:24
  • I updated my question? – M. Fawad Surosh Feb 07 '17 at 17:28
  • @HansPassant, I couldn't find auto-resize for WebBrowser control. Is there anything I am missing? – M. Fawad Surosh Feb 07 '17 at 17:34
  • Possible duplicate of [Size WPF Browser to its HTML-Content](http://stackoverflow.com/questions/40719182/size-wpf-browser-to-its-html-content) – rene Feb 07 '17 at 17:40
  • @rene it doesn't satisfy my need. – M. Fawad Surosh Feb 07 '17 at 17:47
  • Why not? What is not working? – rene Feb 07 '17 at 19:36
  • @rene In my scenario, I am using **WinForm** Control in which I have **WebBrowser** and on the other hand, I couldn't see **LoadCompleted** event instead I can see **DocumentCompleted**. I copied the same code there and I was getting the error: **System.Windows.Forms.HtmlElement' does not contain a definition for 'parentElement'**. Is there any other workaround needs to be done? if yes, please guide. – M. Fawad Surosh Feb 07 '17 at 20:58
  • Since the width of the text displayed often flows to the width of the browser how will this approach ever work? Are you only working with a well defined set of pages? WebBrowser is pretty limited, have you looked into Chromium? – rheitzman Feb 07 '17 at 23:09

1 Answers1

1

I get it only somewhat to work if I set AutoSize property on the form to false and use the following code in the DocumentCompleted event of the webbrowser control, inspired on the WPF Example from Reza Aghaei

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    this.ClientSize = new Size(
            browser.Document.Body.Parent.ScrollRectangle.Width,
            browser.Document.Body.Parent.ScrollRectangle.Height);    
}
Community
  • 1
  • 1
rene
  • 41,474
  • 78
  • 114
  • 152