5

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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
willseward
  • 113
  • 1
  • 1
  • 8

5 Answers5

3

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
VoteCoffee
  • 4,692
  • 1
  • 41
  • 44
Jeff
  • 2,198
  • 3
  • 21
  • 38
  • Don't forget to allow for scroll bars if enabled. – SteveCinq Sep 27 '19 at 03:34
  • 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. – SteveCinq Oct 26 '20 at 23:01
0

Answered here: use WebBrowser.Document.Body.ScrollRectangle.Size instead of WebBrowser.Document.Window.Size. Working like a charm for me.

Community
  • 1
  • 1
ulatekh
  • 1,311
  • 1
  • 14
  • 19
-1
  • Step one would be to see if you can resize the control at all.
  • Step two would be to see if you can find the size that the web site would like
  • Step three would be to take that size, or the max screen size, whichever is less, minus any chrome, and resize the control to that size.
John Saunders
  • 160,644
  • 26
  • 247
  • 397
-1

Your best bet might be to just make the WebBrowser use as much space (Dock.Fill) as possible on the form from the get-go, so if the user full-screen maximizes the app they get plenty of space, and if they want it smaller for smaller sites they can resize your app however they want. This is how all browsers work.

Jared Updike
  • 7,165
  • 8
  • 46
  • 72
-1
var webBrowser = new WebBrowser();
// the magic!!!
webBrowser.Dock = DockStyle.Fill;
form.get_Controls().Add(webBrowser);
webBrowser.Navigate('http://a.com/');
ufologist
  • 27
  • 2