AutoSize and DockStyle.Fill don't mix

The title of this post essentially says it all.  AutoSize and DockStyle.Fill don't mix well together.  Both properties exist to describe the size relationship relative to the rest of the control but they do so in conflicting ways.

AutoSize is a property describing the size of a control relative to it's contents.  Setting this to True will generally cause a control to resize itself so that it takes up only enough room to display it's contents. 

DockStyle.Fill is a property describing the size of a control relative to the size of it's container.  A control will resize to fit all of the empty space in it's parent container with this property set.

In effect these properties represent opposite ways of describing size.  Most controls will prevent you from setting conflicting settings in the designer.  However it's difficult to prevent you from setting conflicting settings between a container and it's contents.

For instance, one combination is the following.  This may seem a little odd at first.  It commonly happens when you are trying to embed a non-autosizing control inside a Container that supports AutoSize.  For instance if you are trying to place a TextBox inside of a FlowLayoutPanel

  • Container
    • AutoSize = true
    • AutoSizeMode = GrowAndShrink
  • Containee
    • Dock = DockStyle.Fill

If you try this code it will often cause the Container Control to shrink to nothing.  Why?  In affect both controls are asking each other how big they should be and with no one having the deciding factor they agree on ... nothing.

Instead for this scenario you should leave the Containee to DockStyle.None and manually set the size. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值