Docked controls placed within TableLayout do not automatically size smaller than their creation size

ε祈祈猫儿з 提交于 2019-12-06 19:43:37

问题


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.


回答1:


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;



回答2:


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



来源:https://stackoverflow.com/questions/11050113/docked-controls-placed-within-tablelayout-do-not-automatically-size-smaller-than

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!