Control Set for Dock.Fill doesn't fill in run mode

JonJacobs

Active member
Joined
Jul 28, 2022
Messages
35
Programming Experience
10+
I have a simple form (not main form) with a WebBrowser control and a StatusStrip. The WebBrowser Dock property is set to Fill. In design mode the WebBrowser behaves as expected and expands/shrinks with the form, stopping at the StatusStrip which simply travels with the bottom of the form (and varies in width with the width of the form. In run mode, the StatusStrip still behaves as expected, but the WebBrowser does not increase in height with the form, leaving bare form exposed. What is going on? How do I fix it?
 
Solution
This sample works with dock fill for me:
C#:
var form = new Form();
var browser = new WebBrowser
{
    Dock = DockStyle.Fill,
    DocumentText = "<body style='border=1px solid red'>sample</body>"
};
var status = new StatusStrip();
status.Items.Add(new ToolStripStatusLabel("status"));
form.Controls.Add(browser);
form.Controls.Add(status);
form.Show();
This sample works with dock fill for me:
C#:
var form = new Form();
var browser = new WebBrowser
{
    Dock = DockStyle.Fill,
    DocumentText = "<body style='border=1px solid red'>sample</body>"
};
var status = new StatusStrip();
status.Items.Add(new ToolStripStatusLabel("status"));
form.Controls.Add(browser);
form.Controls.Add(status);
form.Show();
 
Solution
Thank you, @JohnH, That essentially is what happens in the Designer.cs generated from using the Form design tools, but I will try out your sample code and see if that makes a difference.
Jon
 
As vaguely recall, there are some nuances in the order that controls are added into the container and how that impacts docking and filling. Or I might be thinking of the table layout control... It's been a while.

In general, for new development, you'll want to skip WinForms and use some other UI framework. WinForms is on life support. MS already pull the plug on it once when they started .NET Core, but there was enough industry outcry to force MS to put it back into .NET Core 3.1 but with no new development commitments except for security maintenance.
 
This sample works with dock fill for me:
C#:
var form = new Form();
var browser = new WebBrowser
{
    Dock = DockStyle.Fill,
    DocumentText = "<body style='border=1px solid red'>sample</body>"
};
var status = new StatusStrip();
status.Items.Add(new ToolStripStatusLabel("status"));
form.Controls.Add(browser);
form.Controls.Add(status);
form.Show();

Your code sample eventually led me to a change I needed to make in my event handlers, and the problem was solved. Thank you @JohnH
 
Are you dynamically adding/removing controls as part of your event handler? Seems kind of strange that you needed to change something in your event handlers to fix a docking issue.
 
Back
Top Bottom