Docked controls placed within TableLayout do not a

2019-04-20 06:24发布

This issue is better demonstrated than explained, so I've set up a git repo with Visual Studio 2010 project that be used to see the issue in action.

I have a project where I've added a ComboBox control (comboField) to a TableLayout control (tableLayoutPanel1). I've set the Dock property of the ComboBox to Fill so that it fills the cell of the TableLayout control that it has been placed in. I've also set the Dock property of the TableLayout control to Fill so that it fills the Form that it has been placed on. The width of the ComboBox is currently 193 pixels.

When I run the form and increase its width, the size of the ComboBox increases, as expected. When I reduce the size of the form, the ComboBox reduces in size until it reaches it's original size (193 pixels). At which point, the width of the ComboBox will not decrease any further, resulting in the right-hand side of the control being clipped. This is contrary to what I would expect: the width of the ComboBox will decrease to zero, given that no MinimumSize has been specified. Neither has a MinimumSize been specified for any of the other controls on the form, such as the TableLayout control.

In case it is relevant, the width of the first column of the TableLayout has is set to an Absolute size of 100 pixels, while the width of the second column of the TableLayout has been set to AutoSize.

Can anyone shed any light on why this form is behaving contrary to my expectations and advise on how I can get this working in the way I want? Any help would be much appreciated.

2条回答
Explosion°爆炸
2楼-- · 2019-04-20 06:37

I downloaded your code and did this change. it works now -

Do the 2nd column's SizeType to percent and make it 100%

Designer Code:-

this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));

enter image description here

查看更多
贪生不怕死
3楼-- · 2019-04-20 07:02

The tablelayoutpanel seems to store the initial size of the controls and never make them smaller regardless of docking, anchoring or any other values such as AutoSizeMode.GrowAndShrink. In designer the size of the docked control will always be stored, so the easiest method I've found is to set the width to 0 after the initializecomponent (since the control is docked, it won't actually resize the combobox, but it will allow the tablelayoutpanel to make it as small as the set width)

        InitializeComponent();
        comboField.Width = 0;
查看更多
登录 后发表回答