WebBrowser control resize

24,223

You should be able to access the size of the WebBrowser's Document and get it's size through the WebBrowser.Document.Window.Size.

I would use the WebBrowser Controls's DocumentCompleted event to wait until the page is loaded then use your resize code to resize the webControl container with this size.

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        WebBrowser1.Size = WebBrowser1.Document.Body.ScrollRectangle.Size
    End Sub
Share:
24,223
willseward
Author by

willseward

Updated on March 17, 2020

Comments

  • willseward
    willseward over 4 years

    I have a WebBrowser in my form that accesses a website that is too wide for the control. I would like to know if there is a way to resize the control to fit the website. It seems simple, but i have not found a way yet. I know how to resize the control, I just need to find the width of the website. Just like when you press the green button in safari it automatically resized the window to the correct size.

  • John Saunders
    John Saunders almost 15 years
    Good. Step two. Unfortunately, I don't know how to do that! Sorry.
  • SteveCinq
    SteveCinq over 4 years
    Don't forget to allow for scroll bars if enabled.
  • SteveCinq
    SteveCinq over 3 years
    Since moving to Windows 10, using the ScrollRectangle doesn't seem to accurately set the WebBrowser control size (I end up with scroll bars). I'll post if I find a reliable solution.