0

I try to understand the sizing behaviour of a windows forms window. For example, the following code results in a window that has some margin to the bottom and right.

using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            FlowLayoutPanel verticalPanel = new FlowLayoutPanel();
            verticalPanel.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(verticalPanel);
            AutoSize = true;
        }
    }
}

The code results in the following form:

resulting form of example code

What do I need to do in order to make the window end at the minimum possible size, i.e., at the border of the flow layout panel?

Edit: I don't want to fix the dialog to its minimum size, only set it during initialization.

Amos Egel
  • 937
  • 10
  • 24
  • Window has a [minimum size](https://stackoverflow.com/questions/5314041/set-minimum-window-size-in-c-sharp-net). You "just" have to manually calculate this size. – Dialecticus Dec 08 '21 at 09:47
  • 1
    In real life, it make me mad once I meet a window dictating what size should it take. Please don't! It's me as a user to decide, period. Then the app MUST adapt to the size defined, not opposite. Especially for non-standard screen resolutions, custom DPI, big 4K+ screens... – Yury Schkatula Dec 08 '21 at 09:53
  • Is there a reason you're not using the Windows Forms designer to add the panel? It's much easier that way. Then you can easily choose to anchor the edges of the panel so it resizes when the parent is resized. (It's possible but MUCH more fiddly to do that in code, and I advise that you avoid doing that if you can...) – Matthew Watson Dec 08 '21 at 10:05
  • @MatthewWatson Thanks for the suggestion, but in our case we need to do it programmatically because the number of controls on the form changes based on what the user enters. – Amos Egel Dec 08 '21 at 10:10
  • 1
    As a UI design philosophy that risks leaving users wondering "where's the XYZ textbox? I'm sure it was here last week" – Caius Jard Dec 08 '21 at 10:27
  • @CaiusJard Well the user gets an immediate response. When he or she changes the selected item of a drop down menu, some controls appear / disappear. In general, one could for sure make them active/inactive instead. However that would be pretty messy in our use case. – Amos Egel Dec 08 '21 at 10:58
  • Normally we make the controls inside the window resize when the window is resized (and we start the window at some sensible size); this seems like you're going the other way round, sizing the window to the controls? Personally I'd anchor the panel, set a good size for the window initial and let the user resize it if they want – Caius Jard Dec 08 '21 at 11:10
  • You're creating a new Control in the Constructor of a Form without specifying a behavior (a configuration set that defines a state for the Control's Layout) which will be applied when the LayoutEngine will start working. In the Form's Constructor, neither the TLP or the Form has yet created a Handle. When the Handle is created, the Form will auto-size, then the TLP will size to default values: the Form won't re-auto-size, unless instructed to do so, again -- As noted in other comments, that's not the way to go. – Jimi Dec 08 '21 at 14:54
  • Add `AutoSizeMode = AutoSizeMode.GrowAndShrink;` to allow the form to shrink. – Hans Passant Dec 08 '21 at 15:19

0 Answers0