squillman
squillman

Reputation: 13641

Winform won't resize when setting Size property

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.

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.

The resize is within the WebBrowsers DocumentCompleted event:

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);
}

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:

enter image description here

I also tried converting pixels to points, but this had the same result. Size was still 37x38.

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);
}

The WebBrowser control loads the document during the form's Activated event:

private void WebBrowserView_Activated(object sender, EventArgs e)
{
    ViewWebBrowser.Navigate(URL); 
}

URL is a public string property set by a presenter. The presenter does not set any size properties on the form.

AutoSize on the form is set to false. The only properties on the form that I've changed from the default are Text and FormBorderStyle which is set to SizableToolWindow.

In addition to a new Size structure, I've also tried setting the Height and Width properties independently with the same result.

MinimumSize and MaximumSize are both set to 0,0. Setting MinimumSize to 1,1 does not change anything.

DockStyle on the WebBrowser control is set to Fill so I'm only setting Size on the form.

Why won't the form accept the new Size?

EDIT:

Here is the full class of the form:

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();
    }
}

Upvotes: 4

Views: 3779

Answers (2)

mrogal.ski
mrogal.ski

Reputation: 5930

Judging by the code, everything should work as intended but there is something called pending layout requests in WinForms which requests an update for the layout. These changes are applied after invalidation of the UI so it is recommended to use SuspendLayout method before updating crucial layout/visual elements and then call ResumeLayout to apply these pending layout requests.

To apply these changes simply do :

void ViewWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    SuspendLayout();
    ViewWebBrowser.Height = ViewWebBrowser.Document.Window.Size.Height;
    ViewWebBrowser.Width = ViewWebBrowser.Document.Window.Size.Width;
    Size = new Size(ViewWebBrowser.Width, ViewWebBrowser.Height);
    ResumeLayout();
}

Upvotes: 4

LarsTech
LarsTech

Reputation: 81620

Try using the ScrollRectangle property instead:

ViewWebBrowser.Height = ViewWebBrowser.Document.Body.ScrollRectangle.Height;
ViewWebBrowser.Width = ViewWebBrowser.Document.Body.ScrollRectangle.Width;

Per the comment thread, remove the Dock style:

ViewWebBrowser.Dock = DockStyle.None;

Using Fill will interfere with your height and width measurements.

Upvotes: 0

Related Questions