title: Winform won't resize when setting Size property
body: <p>I have a WinForm that is used to host a WebBrowser control.  I want to dynamically resize the form based on the document size that the browser loads.</p>
  
  <p>I can successfully read the document size from within the WebBrowser control and I set the form size based on that, but the form simply will not resize.</p>
  
  <p>The resize is within the WebBrowsers DocumentCompleted event:</p>
  
  <pre><code>private void ViewWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  {
      ViewWebBrowser.Height = ViewWebBrowser.Document.Window.Size.Height;
      ViewWebBrowser.Width = ViewWebBrowser.Document.Window.Size.Width;
      Size = new Size(ViewWebBrowser.Width, ViewWebBrowser.Height);
  }
  </code></pre>
  
  <p>This event fires just fine, the document loads and the document dimensions are detected as expected and they are correct based on the page I'm loading, but Size is always 37x38 coming out of the event handler.  Here's a screenshot of the debugger at a breakpoint:</p>
  
  <p><a href="https://i.stack.imgur.com/1PiZg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1PiZg.png" alt="enter image description here"></a></p>
  
  <p>I also tried converting pixels to points, but this had the same result.  Size was still 37x38.</p>
  
  <pre><code>private void ViewWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  {
      Graphics g = this.CreateGraphics();
      g.PageUnit = GraphicsUnit.Pixel;
      ViewWebBrowser.Height = Convert.ToInt32(ViewWebBrowser.Document.Window.Size.Height * 72 / g.DpiY);
      ViewWebBrowser.Width = Convert.ToInt32(ViewWebBrowser.Document.Window.Size.Width * 72 / g.DpiX);
      Size = new Size(ViewWebBrowser.Width, ViewWebBrowser.Height);
  }
  </code></pre>
  
  <p>The WebBrowser control loads the document during the form's <code>Activated</code> event:</p>
  
  <pre><code>private void WebBrowserView_Activated(object sender, EventArgs e)
  {
      ViewWebBrowser.Navigate(URL); 
  }
  </code></pre>
  
  <p><code>URL</code> is a public string property set by a presenter.  The presenter does not set any size properties on the form.</p>
  
  <p><code>AutoSize</code> on the form is set to false.  The only properties on the form that I've changed from the default are <code>Text</code> and <code>FormBorderStyle</code> which is set to <code>SizableToolWindow</code>.</p>
  
  <p>In addition to a new <code>Size</code> structure, I've also tried setting the <code>Height</code> and <code>Width</code> properties independently with the same result.</p>
  
  <p><code>MinimumSize</code> and <code>MaximumSize</code> are both set to 0,0.  Setting <code>MinimumSize</code> to 1,1 does not change anything.</p>
  
  <p><code>DockStyle</code> on the WebBrowser control is set to <code>Fill</code> so I'm only setting <code>Size</code> on the form.</p>
  
  <p>Why won't the form accept the new Size?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Here is the full class of the form:</p>
  
  <pre><code>public partial class WebBrowserView : Form, IWebBrowserView
  {
      public WebBrowserView()
      {
          InitializeComponent();
      }
  
      public string URL { private get; set; }
  
      private void WebBrowserView_Activated(object sender, EventArgs e)
      {
          ViewWebBrowser.Navigate(URL);
      }
  
      private void ViewWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
      {
          var newHeight = ViewWebBrowser.Document.Window.Size.Height;
          var newWidth = ViewWebBrowser.Document.Window.Size.Width;
          ViewWebBrowser.Height = newHeight;
          ViewWebBrowser.Width = newWidth;
          this.Size = new Size(ViewWebBrowser.Width, ViewWebBrowser.Height);
      }
  
      private void WebBrowserView_FormClosing(object sender, FormClosingEventArgs e)
      {
          ViewWebBrowser.Dispose();
      }
  }
  </code></pre>